Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 227
csg Avatar asked May 24 '20 15:05

csg


People also ask

Can you choose output bound as a Cosmos DB in Azure function?

The Azure Cosmos DB output binding lets you write a new document to an Azure Cosmos DB database using the SQL API. For information on setup and configuration details, see the overview.

How do you read a Cosmos DB from Azure function?

The Azure Cosmos DB input binding uses the SQL API to retrieve one or more Azure Cosmos DB documents and passes them to the input parameter of the function. The document ID or query parameters can be determined based on the trigger that invokes the function.

How many input binding is an Azure function allowed?

A trigger defines how the function is called upon; however, every function has one trigger and optional bindings.


1 Answers

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.

like image 67
simandibalazs Avatar answered Sep 18 '22 09:09

simandibalazs