gpt4 book ai didi

c# - Azure函数错误: Microsoft. Azure.WebJobs.Host : Error indexing method 'Function1' .无法将参数 'document'绑定(bind)到类型IAsyncCollector`

转载 作者:行者123 更新时间:2023-12-03 04:46:13 25 4
gpt4 key购买 nike

我是 Azure Function 的新手,在我的第一个函数中我使用的是 CosmosDB。在幕后,函数正在完美地完成其工作,但是当我在门户中打开函数时,我收到此错误。

Function (LOANGILITY-AZFUNCTION/ProductDetailsFunc) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'ProductDetailsFunc'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'document' to type IAsyncCollector`1. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

我的函数头原型(prototype)是

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.

最佳答案

根据我的测试,我们可以通过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;
}
}
  • 部署到 Azure enter image description here

  • 配置应用程序设置

  • enter image description here

    enter image description here

  • 测试 enter image description here
  • enter image description here

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

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

    关于c# - Azure函数错误: Microsoft. Azure.WebJobs.Host : Error indexing method 'Function1' .无法将参数 'document'绑定(bind)到类型IAsyncCollector`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57989264/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com