在Async方法中绑定到输出Blob时,将Blob绑定到IAsyncCollector时出错

nas*_*iar 2 c# asynchronous azure azure-storage-blobs azure-functions

我在这篇文章之后尝试在Async方法中绑定到Blob输出:如何将输出值绑定到我的异步Azure Function?

我有多个输出绑定,所以仅返回不是一种选择

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, IAsyncCollector<string> collection, TraceWriter log)
{
    if (req.Method == HttpMethod.Post) 
    {
        string jsonContent = await req.Content.ReadAsStringAsync();

        // Save to blob 
        await collection.AddAsync(jsonContent);

        return req.CreateResponse(HttpStatusCode.OK);
    }
    else 
    {
        return req.CreateResponse(HttpStatusCode.BadRequest);

    }
}
Run Code Online (Sandbox Code Playgroud)

我对Blob的绑定是:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "blob",
      "name": "collection",
      "path": "testdata/{rand-guid}.txt",
      "connection": "test_STORAGE",
      "direction": "out"
    }
  ],
  "disabled": false
}
Run Code Online (Sandbox Code Playgroud)

但是每当我这样做时,我都会得到以下信息:

错误:函数($ WebHook)错误:Microsoft.Azure.WebJobs.Host:索引方法'Functions.WebHook'错误。Microsoft.Azure.WebJobs.Host:无法将Blob绑定为类型“ Microsoft.Azure.WebJobs.IAsyncCollector`1 [System.String]”

Mik*_*kov 5

Blob输出绑定不支持收集器,请参见此问题

对于可变数量的输出Blob(在您的情况下为0或1,但可以是任意数量),您将必须使用命令式绑定。collection从您的绑定中删除绑定function.json,然后执行以下操作:

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, Binder binder)
{
    if (req.Method == HttpMethod.Post) 
    {
        string jsonContent = await req.Content.ReadAsStringAsync();

        var attributes = new Attribute[]
        {    
            new BlobAttribute("testdata/{rand-guid}.txt"),
            new StorageAccountAttribute("test_STORAGE")
        };

        using (var writer = await binder.BindAsync<TextWriter>(attributes))
        {
            writer.Write(jsonContent);
        }

        return req.CreateResponse(HttpStatusCode.OK);
    }
    else 
    {
        return req.CreateResponse(HttpStatusCode.BadRequest);    
    }
}
Run Code Online (Sandbox Code Playgroud)