Jesper
Jesper

Reputation: 25

Running Azure Functions with Cosmos DB locally - cannot find CosmosDBAttribute.ConnectionStringSetting

The following error appears when trying to run the function: Unable to resolve the value for property 'CosmosDBAttribute.ConnectionStringSetting'. Make sure the setting exists and has a valid value.

That property exists both in the local.settings.json and in my Application Settings on Azure.

One important bit: It works on my machine now, but it didn't use to work. It started working out of nowhere even when I had not made any changes. It still doesn't work for others trying to run it on their machines.

Another important bit: They CAN get it to work by adding the attribute to their environment variables on their PC, but that is just a workaround.

I'm using Windows 10 and using Intellij as my IDE.

This is the code of the function I'm trying to run:

@FunctionName("postLogItem")
public HttpResponseMessage post(
        @HttpTrigger(name = "req",
                methods = {HttpMethod.GET, HttpMethod.POST},
                authLevel = AuthorizationLevel.ANONYMOUS)
                HttpRequestMessage<Optional<String>> request,
        @CosmosDBOutput(
                name = "document",
                databaseName = "AuditLog",
                collectionName = "Logs",
                connectionStringSetting = "CosmosDBAttribute.ConnectionStringSetting")
                OutputBinding<String> document,
        final ExecutionContext context) {

    // Item list
    context.getLogger().info("Parameters are: " + System.getenv("CosmosDBAttribute.ConnectionStringSetting"));

    // Parse query parameter
    String query = request.getQueryParameters().get("desc");
    String name = request.getBody().orElse(query);
    // Generate random ID
    final int id = Math.abs(new Random().nextInt());

    // Generate document
    final String jsonDocument = "{\"id\":\"" + id + "\", " +
            "\"description\": \"" + name + "\"}";

    context.getLogger().info("Document to be saved: " + jsonDocument);

    //LogItem item = new LogItem("123", "101010", "{'name':'potato'}", LogItem.TYPE.PAYMENT);
    document.setValue(jsonDocument);

    return request.createResponseBuilder(HttpStatus.OK)
            .body("Document created successfully.")
            .build();
}

Upvotes: 0

Views: 1115

Answers (1)

SaiSakethGuduru
SaiSakethGuduru

Reputation: 2440

Unable to resolve the value for property 'CosmosDBAttribute.ConnectionStringSetting'. Make sure the setting exists and has a valid value.

for the above error I have tested in my local environment and provided the code make changes in your code accordingly.

In your host.json file, remove the dot (.) in the connection string variable and keep underscore(_) and then use the same variable in your function class file as you can see my connection string variable above in both host.json file and function class file.

**local.settings.json**  

  

{  
"IsEncrypted": false,  
"Values": { "  
AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet",  
"cosmosdbtvk_DOCUMENTDB": "<PRIMARY CONNECTION STRING OF SOURCE COLLECTION>",  
"endpointUrl":"<URI OF TARGET COLLECTION>",  
"primaryKey": "<PRIMARY KEY OF TARGET COLLECTION>"  
}  
}

  

**Class file:**

  

  
[FunctionName("CosmosDBTriggerCSharp1")]  
public static async Task Run([CosmosDBTrigger(  
databaseName: "database",  
collectionName: "collection1",  
ConnectionStringSetting = "cosmosdbtvk_DOCUMENTDB", LeaseCollectionName = "leases", CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> input, ILogger log)

Upvotes: 1

Related Questions