Search code examples
azure-functionsazure-cosmosdb

Azure Cosmos DB input binding for an Azure Function doesn't work


I was following a walkthrough from microsoft learning on how to add an input binding with CosmosDb for an azure function, however when calling the function it keeps returning internal server error (500 http code).

The configuration of the azure function from function.json is:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "Request",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "Response"
    },
    {
      "name": "bookmark",
      "direction": "in",
      "type": "cosmosDB",
      "databaseName": "func-io-learn-db",
      "collectionName": "Bookmarks",
      "connectionStringSetting": "learn_DOCUMENTDB",
      "id": "{id}",
      "partitionKey": "{id}",
      "sqlQuery": ""
    }
  ]
}

There is a learn_DOCUMENTDB configuration settings in the app service which has a valid connection string to cosmos db instance (was automatically created).

The error log entry says that:

Can't bind CosmosDB to type 'System.String'. Possible causes: 1) Tried binding to 'Microsoft.Azure.Documents.Client.DocumentClient, Microsoft.Azure.DocumentDB.Core, Version=2.9.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' but user type assembly was 'System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e.

Is there something I do wrong?


Solution

  • I had the same issue, turns out the new UI generates a different binding than the old one.

    New UI:

    {
      "name": "bookmark",
      "direction": "in",
      "type": "cosmosDB",
      "databaseName": "func-io-learn-db",
      "collectionName": "Bookmarks",
      "connectionStringSetting": "learn-0088a129-899f-4d18-b4db-5fa74daf1cc3_DOCUMENTDB",
      "id": "{id}",
      "partitionKey": "{id}",
      "sqlQuery": ""
    }
    

    Old UI:

    {
      "type": "cosmosDB",
      "name": "bookmark",
      "databaseName": "func-io-learn-db",
      "collectionName": "Bookmarks",
      "connectionStringSetting": "learn-0088a129-899f-4d18-b4db-5fa74daf1cc3_DOCUMENTDB",
      "id": "{id}",
      "partitionKey": "{id}",
      "direction": "in"
    }
    

    Removing the

    "sqlQuery": ""
    

    part from the binding fixed it for me.

    You can switch back to the old UI by clicking the "Having issues? Click to go back to the classic Function App management experience" on the app service overview page, as you can see here.