Error binding Blob to IAsyncCollector when binding to output blob in Async method(在 Async 方法中绑定到输出 blob 时,将 Blob 绑定到 IAsyncCollector 时出错)
问题描述
我正在尝试在这篇文章之后以异步方法绑定到 blob 输出:如何将输出值绑定到我的异步 Azure 函数?
I'm trying to bind to a blob output in an Async method following this post: How can I bind output values to my async Azure Function?
我有多个输出绑定,所以只返回不是一个选项
I have multiple output bindings so just returning is not an option
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);
    }
}
我对 blob 的绑定是:
My binding for the blob is :
{
  "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
}
但每当我这样做时,我都会得到以下信息:
But whenever I do this I get the following:
错误:函数($WebHook)错误:Microsoft.Azure.WebJobs.Host:索引方法错误'Functions.WebHook'.Microsoft.Azure.WebJobs.Host:无法绑定要键入的 Blob'Microsoft.Azure.WebJobs.IAsyncCollector`1[System.String]'
Error: Function ($WebHook) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.WebHook'. Microsoft.Azure.WebJobs.Host: Can't bind Blob to type 'Microsoft.Azure.WebJobs.IAsyncCollector`1[System.String]'
推荐答案
Blob 输出绑定不支持收集器,请参阅此 问题.
Collectors are not supported for Blob output bindings, see this issue.
对于可变数量的输出 blob(在您的情况下为 0 或 1,但可以是任意数量),您必须使用命令式绑定.从您的 function.json 中删除 collection 绑定,然后执行以下操作:
For variable amount of output blobs (0 or 1 in your case, but can be any), you would have to use imperative bindings. Remove collection binding from your function.json and then do this:
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);    
    }
}
这篇关于在 Async 方法中绑定到输出 blob 时,将 Blob 绑定到 IAsyncCollector 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Async 方法中绑定到输出 blob 时,将 Blob 绑定到 IAsyncCollector 时出错
 
				
         
 
            
        - C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 
						 
						 
						 
						 
						 
				 
				 
				 
				