2

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 Answers1

1

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.

Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
  • Just to add to Jerry's answer, you don't need to do a POST to trigger the Function, the whole point of the Cosmos DB Trigger is that the Function triggers when you insert or update a document in the source collection. – Matias Quaranta Jul 06 '18 at 13:54
  • @MatiasQuaranta Thanks, nice complement. It seems OP tried to follow exactly the [tutorial](https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local) to test whether it works. – Jerry Liu Jul 06 '18 at 13:57
  • Thank you all. Works perfectly now! – user2248614 Jul 07 '18 at 09:01