Reputation: 2424
I am using the portal to create an HttpTriggered Azure Function that should insert a document in CosmosDB. I use all the defaults and when I configure the binding for output to Cosmos, I use the integration page to specify it..
Ok so my code for the function is:
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
public static IActionResult Run(HttpRequest req, out dynamic outputDocument, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
outputDocument = new { Description = name, id = Guid.NewGuid() };
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
and the Function.json is this:
{
"bindings": [
{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get",
"post"
]
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"name": "outputDocument",
"databaseName": "outDatabase",
"collectionName": "MyCollection",
"createIfNotExists": true,
"connectionStringSetting": "johncosmosgorter_DOCUMENTDB",
"partitionKey": "id",
"direction": "out",
"type": "cosmosDB"
}
]
}
Notice that the binding parameters where generated by the integration page, I did not write this by hand.
Now when we run the function, we get an 500 error, the logs show this:
Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting.
This is super strange, since I did not generate the binding. It looks like a version 3 vs version 4 binding parameter conflict, because when I change the binding to have the connectionStringSetting to become connection it actually works and gives me the next error, which is
Value cannot be null. (Parameter 'databaseId')
The binding has a databaseName, changing that to databaseId should not be the solution. Doing that does not work either.
What is wrong here, why is the super simple boilerplate cosmosDB output binding for Azure Functions not working? And why is it so hard to get correct guidance?
Thank in advance for any insight!
John.
Upvotes: 2
Views: 1602
Reputation: 6497
Notice that the binding parameters where generated by the integration page, I did not write this by hand. Now when we run the function, we get an 500 error.
Yes, I do agree with you, while creating the cosmos DB output binding using the Integration page, binding parameters are getting generated incorrectly. These parameters are not pointing to v4.
As per ms docs, connection should be used instead of connectionStringSetting and containerName should replace collectionName for v4.
But instead it's generating below attributes-
{
"name": "outputDocument",
"direction": "out",
"type": "cosmosDB",
"connectionStringSetting": "afreen-cosmosdb_DOCUMENTDB",
"databaseName": "outDatabase",
"collectionName": "MyCollection",
"createIfNotExists": true,
"partitionKey": "/id"
}
Even I was getting this Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting.
error and got rid of it after correcting connectionStringSetting to connection.
Then I also started getting Value cannot be null. (Parameter 'databaseId')
error, later I updated collectionName to containerName and it started working for me.
Final binding attributes looks like below which worked for me-
{
"name": "outputDocument",
"direction": "out",
"type": "cosmosDB",
"connection": "afreen-cosmosdb_DOCUMENTDB",
"databaseName": "outDatabase",
"containerName": "MyCollection",
"createIfNotExists": true,
"partitionKey": "/id"
}
I have got the output.
how do I know which version they are using
You can check the function's version from here.
I have raised a bug with the product team, you can check it for further updates.
Upvotes: 5