Search code examples
c#azureazure-functionsazure-cosmosdbazure-webjobs

Error indexing method 'Function1'. Cannot bind parameter 'document' to type IAsyncCollector`


I am getting an Azure Function Error for Microsoft.Azure.WebJobs.Host. In my function I'm using CosmosDB. in debug/local its job perfectly but when I open my function in portal I'm getting this error.

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.).

My Function Header Prototype is

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)

Generated json from my code is

{
  "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"
}

I also see some other SO questions and they are talking about builder.something in the code and I'm not working on .Net Core Azure Function, my target Project Framework is 4.6.1.


Solution

  • According to my test, we can directly deploy Function to Azure via visual studio. But we need to manually configure some settings in local.settings.json such as Cosmos Db connection string. The detailed steps are as below

    1. Develop My Code:
     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. Deploy to Azure enter image description here

    2. Configure application settings

    enter image description here

    enter image description here

    1. Test enter image description here

    enter image description here

    Update Regarding the issue, it may be that you do not add your cosmos db connection string into application settings. Please check if you have added. enter image description here

    Besides if you have added it, you still have the error. You check you logs to get the detailed error message. enter image description here