【问题标题】:Azure Function Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1'. Cannot bind parameter 'document' to type IAsyncCollector`Azure 函数错误:Microsoft.Azure.WebJobs.Host:错误索引方法“Function1”。无法将参数“文档”绑定到类型 IAsyncCollector`
【发布时间】:2019-09-18 09:19:02
【问题描述】:

我是 Azure Function 的新手,在我的第一个函数中我使用的是 CosmosDB。在引擎盖下,函数完美地完成了它的工作,但是当我在门户中打开我的函数时,我收到了这个错误。

函数 (LOANGILITY-AZFUNCTION/ProductDetailsFunc) 错误: Microsoft.Azure.WebJobs.Host:索引方法错误 'ProductDetailsFunc'。 Microsoft.Azure.WebJobs.Host:无法绑定 参数“文档”以键入 IAsyncCollector`1。确保 绑定支持参数类型。如果您使用绑定 扩展(例如 Azure 存储、ServiceBus、计时器等)确保 您已经调用了您的扩展程序的注册方法 启动代码(例如 builder.AddAzureStorage()、builder.AddServiceBus()、 builder.AddTimers() 等)。

我的函数头原型是

public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req,
        [DocumentDB(
            databaseName: "OB",
            collectionName: "ProductDetails",
            ConnectionStringSetting = "DBConnection")]IAsyncCollector<dynamic> document,
        TraceWriter log)

从我的代码生成的 json 是

{
  "generatedBy": "Microsoft.NET.Sdk.Functions-1.0.13",
  "configurationSource": "attributes",
  "bindings": [
    {
      "type": "httpTrigger",
      "methods": [
        "get",
        "post"
      ],
      "authLevel": "anonymous",
      "name": "req"
    }
  ],
  "disabled": false,
  "scriptFile": "../bin/Loangility01.dll",
  "entryPoint": "Loangility01.ProductDetailsFunc.Run"
}

我还看到其他一些 SO 问题,他们在代码中谈论 builder.something,而我没有使用 .Net Core Azure Function,我的目标项目框架是 4.6.1。

【问题讨论】:

  • 你使用的是Azure Function V1.0吗?
  • 是的,我正在使用 v1
  • 您想使用 Azure Cosmos DB 作为输入或输出绑定吗?
  • 朋友是的,我在我的天蓝色函数中使用 cosmos db,它基本上是输出绑定,甚至我成功地将我的数据保存在 cosmos db 中。唯一的问题是我在门户中遇到了这个错误。

标签: c# azure azure-functions azure-cosmosdb


【解决方案1】:

根据我的测试,我们可以通过visual studio直接将Function部署到Azure。但是我们需要在 local.settings.json 中手动配置一些设置,例如 Cosmos Db 连接字符串。具体步骤如下

  1. 开发 我的代码:
 public static class Function2
    {
        [FunctionName("Function2")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, [DocumentDB(
                databaseName: "ToDoItems",
                collectionName: "Items",
                ConnectionStringSetting = "CosmosDBConnection")]IAsyncCollector<dynamic> toDoItemsOut, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            // parse query parameter
            string name = req.GetQueryNameValuePairs()
                .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                .Value;

            if (name == null)
            {
                // Get request body
                dynamic data = await req.Content.ReadAsAsync<object>();
                name = data?.name;
            }
            HttpResponseMessage response ;



            if (name == null) {
                response = req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body");

            }
            else {

                response= req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
            }
            await toDoItemsOut.AddAsync(response.Content);
            return response;
        }
    }
  1. 部署到 Azure

  2. 配置应用程序设置

  1. 测试

更新 关于这个问题,可能是您没有将 cosmos db 连接字符串添加到应用程序设置中。请检查您是否已添加。

如果你添加了它,你仍然有错误。您检查日志以获取详细的错误消息。

【讨论】:

  • 朋友 我已将我的 Azure 门户功能版本运行时更新到 v1,我的问题得到了解决。但现在我的功能是给我500 internal server 并且不执行任何操作。
【解决方案2】:

我们需要注意的事情很少。

  1. 即使在 v1 for Cosmos DB 中,您也必须手动安装在知道如何与 CosmosDB 集成的扩展中。此文档链接到要安装到项目中的包link
  2. 在Azure Portal中创建Azure函数时,默认为版本2;当我们使用 v1 Azure 功能link 时,我们需要手动配置 v1

【讨论】:

  • 朋友Microsoft.Azure.WebJobs.Extensions.DocumentDB 1.2.0 已经安装在我的项目中。
  • 听起来只有在云中运行时才会发生错误。您能否确认云中的功能设置为 ~1 而不是 ~2
猜你喜欢
  • 2019-07-13
  • 2020-04-05
  • 2018-09-15
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 2017-11-07
  • 2017-04-27
  • 2023-01-12
相关资源
最近更新 更多