toshiomagic
toshiomagic

Reputation: 1649

CosmosDBTrigger fails with binding error due to incorrect type

I have an already existing cosmos db and I want to use it to trigger an azure function.

Here's my Azure Function code:

using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System.Collections.Generic;

namespace CosmosTriggerFunc
{
    public static class MyFunc
    {
        [FunctionName("Func")]
        public static void Run([CosmosDBTrigger(
            databaseName: "dbname",
            collectionName: "collectionname",
            ConnectionStringSetting = "connectionStringSetting",
            LeaseCollectionName = "leases",
            CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> documents,
            TraceWriter log)
        {
            if (documents != null && documents.Count > 0)
            {
                log.Info($"Documents modified: {documents.Count}");
                log.Info($"First document Id: {documents[0].Id}");
            }
        }
    }
}

And here's my local.settings.json:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "<validstring>",
    "AzureWebJobsDashboard": "<validstring>",
    "type": "CosmosDBTrigger",
    "name": "documents",
    "direction": "in",
    "leaseCollectionName": "leases",
    "connectionStringSetting": "<valid-conn-string>",
    "createLeaseCollectionIfNotExists": true
  }
}

And here is my error:

A ScriptHost error has occurred
Exception while executing function 
Exception binding parameter 'documents'.
Binding can only be done with IReadOnlyList<Document> or JArray
Parameter name: type.

I have a class in another project which are the documents in this collection. But I tried inserting that where the Document class is currently used and I got the same error.

Upvotes: 1

Views: 1350

Answers (2)

abhiroop mukherjee
abhiroop mukherjee

Reputation: 69

Try and create the trigger function from the scratch. Best way to resolve this.

Upvotes: 0

Matias Quaranta
Matias Quaranta

Reputation: 15603

The Cosmos DB Azure Function Extension nuget package already comes with a reference to the Cosmos DB SDK.

This error often happens if you are manually adding a Cosmos DB SDK nuget package to one of your projects which has a different version than the one defined by the Azure Function Extension.

To solve this, remove the Cosmos DB SDK reference that was manually added and let it resolve through the Extension.

Upvotes: 4

Related Questions