0
votes

I'm new to Azure Function and in my first function I'm using CosmosDB. Under the hood, function is doing 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.

2
Is that you use Azure Function V1.0?Jim Xu
yes I'm using v1Usama Shahid
Is that you want to use Azure Cosmos DB as input or output binding?Jim Xu
Friend yes I'm working with cosmos db in my azure function and it is basically output binding and even I'm saving my data in cosmos db successfully. Only the issue is I'm facing this error in my portal.Usama Shahid

2 Answers

4
votes

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

1
votes

There are few things that we need to be taken care of.

  1. Even in v1 for Cosmos DB specifically you have to manually install in the extension that knows how to integrate with CosmosDB. This doc links to the package to install into the project link
  2. By default when we create Azure function in Azure Portal, it is of version 2; and we need to manually configure for v1 when we're working with v1 Azure function link