2
votes

I am trying to run an azure function locally on my laptop using the Azure Functions Core Tools. Notice that this function is configured as a cosmosDB trigger

I was partially inspired by the instructions in this tutorial

I started by creating a function called MyFirstFunction with the following commands (and inserting the required inputs when prompted):

func init
func start

My generated javascript function is (the same the Azure portal creates for the same kind of template function):

module.exports = function (context, documents) {
    if (!!documents && documents.length > 0) {
        context.log('Document Id: ', documents[0].id);
    }
    context.done();
}

My generated function.json is:

{
    "bindings":
    [
        {
          "type": "cosmosDBTrigger",
          "name": "documents",
          "direction": "in",
          "leaseCollectionName": "leases"
        }
    ]
}

My generated local.settings.json is

{
    "IsEncrypted": false,
    "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true"
    }
}

Given this setup I try to run the function by executing:

func host start

Everything runs fine in the console output until the error message: Unable to configure binding 'documents' of type 'cosmosDBTrigger'. This may indicate invalid function.json properties. Can't figure out which ctor to call.

What I missing? I was supposed to trigger the function through an http POST to:

http://localhost:{port}/admin/functions/{function_name}

as explained in the tutorial linked above (being this function a cosmosDB trigger) but the function cannot even be loaded after this error.

What am I missing to run a cosmosDB trigger locally?

Many thanks.

1
notice that I have also run: func extensions install before starting the function.user2248614
notice that if I follow the same procedure creating a HTTP trigger it works fine.user2248614
Is this a v1 or v2 function app?Connor McMahon
It’s a V2 function appuser2248614

1 Answers

1
votes

The problem is your local.settings.json and function.json lack necessary configuration of cosmosdb connection string.

See cosmosdb trigger document.

function.json

{
    "bindings":
    [
        {
          "type": "cosmosDBTrigger",
          "name": "documents",
          "direction": "in",
          "leaseCollectionName": "leases",

          // Missed in your code
          "connectionStringSetting": "CosmosDBConnectionString",
          "databaseName": "<Get db name>",
          "collectionName": "<Get coll name>",
          "createLeaseCollectionIfNotExists": true
        }
    ]
}

local.settings.json

 {
    "IsEncrypted": false,
        "Values": {
        "FUNCTIONS_WORKER_RUNTIME": "node",
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",

         //Missed in your code
        "CosmosDBConnectionString":"<Get connection string from Azure portal>"
    }
 }

Note that in latest version of function core tools(2.0.1-beta.31), no need to register CosmosDB extension if you use func new to create CosmosDB Trigger, tools will install it automatically.

After this problem solved, if you met another error about Microsoft.Azure.Documents.ServiceInterop.dll

The listener for function 'Functions.CosmosDb' was unable to start.
The listener for function 'Functions.CosmosDb' was unable to start. System.Private.CoreLib: One or more errors occurred. (Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)). 
Microsoft.Azure.DocumentDB.Core: Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E).

Just install one package to fix(See this issue)

func extensions install -p Microsoft.Azure.DocumentDB.Core -v 2.0.0-preview

Then everything should work.