0

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();
}
Jesper
  • 3
  • 2
  • 1
    The value of `connectionStringSetting` is normally the name of the setting (either in local json or created in the Function's Connection String section). You are putting `CosmosDBAttribute.ConnectionStringSetting` the problem might be the dot. Have you tried with something like `CosmosDBConnection` and then using that as the Connection String or Setting name in the Function App? – Matias Quaranta Nov 30 '21 at 15:05
  • This did not work. Let me clarify something, "CosmosDBAttribute.ConnectionStringSetting" is something that appeared in the error message and is not some value that I had defined. I only defined it afterwards. Originally I used a value like CosmosDbConnection. I should add that I am using Java, maybe that's what's causing some issue? – Jesper Dec 02 '21 at 09:06
  • The value is appearing in the error message because it's in the code you shared: `connectionStringSetting = "CosmosDBAttribute.ConnectionStringSetting"`, you are telling Functions that you want the connection string to be read from a setting called `CosmosDBAttribute.ConnectionStringSetting`. The dot potentially is causing the problem, so I would change it to `connectionStringSetting = "MyCosmosConfig"` or something simple, and create a config with that name in the Function App. – Matias Quaranta Dec 02 '21 at 15:07
  • Like I said, I only defined it after I saw it in the error message. Also, I tried what you just described earlier today and it didn't fix it. – Jesper Dec 02 '21 at 20:06
  • Are you defining them on the Application Settings or Connection Strings sections of the Function App? Have you tried defining it on the other? – Matias Quaranta Dec 02 '21 at 20:09
  • It seems the issue was that the Java version differed between what we had locally (the version in our JAVA_HOME) and what the function was running. Once we matched them to both use Java 11 it works. – Jesper Dec 06 '21 at 10:15

1 Answers1

1

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)
SaiSakethGuduru
  • 2,218
  • 1
  • 5
  • 15
  • If my answer is helpful for you, you can accept it as answer( click on the check mark beside the answer to toggle it from greyed out to filled in.). This can be beneficial to other community members. Thank you – SaiSakethGuduru Dec 02 '21 at 04:04
  • Hi, this did not work. Let me clarify something, "CosmosDBAttribute.ConnectionStringSetting" is something that appeared in the error message and is not some value that I had defined. I only defined it afterwards. Originally I used a value like CosmosDbConnection. I should add that I am using Java, maybe that's what's causing some issue? – Jesper Dec 02 '21 at 09:06