Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CosmosDBTrigger: Where to specify connection string?

Using Visual Studio 2017, I created a new Azure Function app. I added a function and one of the attribute parameters is ConnectionStringSetting. That should be a reference to a setting stored somewhere, but I can't figure out where for the life of me.

When I try to debug the method, this is what I get back: enter image description here

I have tried to put it in the local.settings.json file, no luck. I have tried to add an app.config/appSettings section and that doesn't do anything, either.

I am not doing anything crazy in the method:

namespace MyFunctions
{
    public static class TestUpdated
    {
        [FunctionName("DocumentUpdated")]
        public static void Run(
            [CosmosDBTrigger("mydb", "somecollection",
            ConnectionStringSetting = "DbConnString",
            LeaseCollectionName = "lease-test-trigger",
            CreateLeaseCollectionIfNotExists = true)]
            IReadOnlyList<Document> documents, TraceWriter log)
        {
            log.Info("Documents modified " + documents.Count);
            log.Info("First document Id " + documents[0].Id);
        }
    }
}

All my extensions and nuget packages are on the latest versions.

So, how the heck do you set the connection string? It's been hours trying different things and nothing works.

like image 319
Andy Avatar asked Dec 04 '17 03:12

Andy


People also ask

What is connectionstringsetting in cosmosdbconnection?

In the code above, ConnectionStringSetting is the name of the setting that contains the connection string. In this case, it is CosmosDBConnection, so you would want to make sure that setting is properly set in your Functions configuration.

What is the cosmosdbtriggerattribute?

Both in-process and isolated process C# libraries use the CosmosDBTriggerAttribute to define the function. C# script instead uses a function.json configuration file. The name of an app setting or setting collection that specifies how to connect to the Azure Cosmos DB account being monitored. For more information, see Connections.

How can multiple functions be configured to use a Cosmos DB trigger?

If multiple functions are configured to use a Cosmos DB trigger for the same collection, each of the functions should use a dedicated lease collection or specify a different LeaseCollectionPrefix for each function. Otherwise, only one of the functions will be triggered. For information about the prefix, see the Configuration section.

How do I specify the MongoDB connection string?

You can specify the MongoDB connection string using either: the DNS Seed List Connection Format. This section describes the standard format of the MongoDB connection URI used to connect to a MongoDB deployment: standalone, replica set, or a sharded cluster.


1 Answers

public static void Run(
[CosmosDBTrigger("mydb", "somecollection",
ConnectionStringSetting = "DbConnString",
LeaseCollectionName = "lease-test-trigger",
CreateLeaseCollectionIfNotExists = true)]
IReadOnlyList<Document> documents, TraceWriter log)

Per my experience , ConnectionStringSetting should be set in App Settings.

The config would be:

{
    "IsEncrypted": false,
    "Values": {
        "DbConnString": "...your cosmos db string..."
    }
}
like image 68
Jay Gong Avatar answered Oct 28 '22 08:10

Jay Gong