绑定到 C# 预编译 Azure Functions 中的自定义输入属性

Bind to custom input properties in C# Precompiled Azure Functions

我正在尝试使用自定义绑定和模式创建一个 HTTP 触发的 Azure 函数,该函数从 Blob 存储获取文件。在我的例子中,POST HTTP 请求会知道附件名称。 在 this article 文档中讨论了使用 csx 脚本的模式,我在 Visual Studio 中使用了预编译的 C# Azure Functions。我试图将其翻译成以下内容,但它引发了 运行 次异常:

The following 1 functions are in error: Run: Microsoft.Azure.WebJobs.Host: Error indexing method 'HttpTriggerGetAttachmentBlob.Run'. Microsoft.Azure.WebJobs.Host: No binding parameter exists for 'Attachment'.

代码如下:

    public class BlobInfo
    {
        public string Attachment { get; set; }
    }
    public static class HttpTriggerGetAttachmentBlob
    {
        [FunctionName("HttpTriggerGetAttachmentBlob")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function,
            "post")]
            HttpRequestMessage req, 
            TraceWriter log,
            [Blob("strings/{Attachment}")] string blobContents,
            BlobInfo blobInfo)
        {
            if(blobContents == null) {
                return req.CreateResponse(HttpStatusCode.NotFound);
            }

            return req.CreateResponse(HttpStatusCode.OK, new
            {
                data = $"{blobContents}"
            });
        }
    }

如何在知道来自触发事件的文件名的情况下检索 Blob 存储文件?

技巧实际上是将 HttpTrigger 属性放在 blobInfo 参数上,而不是 req:

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req,
  TraceWriter log,
  [Blob("strings/{Attachment}")] string blobContents,
  [HttpTrigger(AuthorizationLevel.Function, "post")] BlobInfo blobInfo)

这将告诉运行时您正在绑定到 BlobInfo class,并且它将能够派生 {Attachment} 模板绑定。

注意:请务必在重新编译之前删除 bin 文件夹,该工具并不总是正确更新现有的 function.json 文件。