I built a .net 8 V4 Azure function app that uses both cosmos db triggers and bindings to read and write data to cosmos db. Locally, the app works fine because I have set up the connection string local.setting.json. However, when I upload the code to Azure, the functions run but it does not communicate with my cosmos db. In the portal, I added the connection string through Environmental variables, and then connection strings, but it did not work. Please one can help to solve this issue.
public CosmosDBFunction(ILogger<CosmosDBFunction> logger,
IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory.CreateClient();
_logger = logger;
_cosmosClient =
new CosmosClient("AccountEndpoint=<Connection>");
}
[Function(nameof(Function))]
public static async Task<string> RunOrchestrator(
[OrchestrationTrigger] TaskOrchestrationContext context)
{
ILogger logger = context.CreateReplaySafeLogger(nameof(CosmosDBFunction));
logger.LogInformation("----Function Started-----");
string pagesToLinks = await context.CallActivityAsync<string>(
nameof(somefunction));
string linksToClins = await context.CallActivityAsync<string>(
nameof(somefunction));
string Blinks = await context.CallActivityAsync<string>(
nameof(somefunction));
logger.LogInformation("----Function Finished-----");
return "Operation Is Completed";
}
[Function(nameof(SomeFunction))]
[CosmosDBOutput(databaseName: "X", containerName: "Y",
Connection = "ConnectionNameSavedInLocal.Setting.json"
, CreateIfNotExists = true, PartitionKey = "/dd")]
public async Task<string> ScanLinksAndInsertIntoLinksContainer
([ActivityTrigger] string name,
[CosmosDBInput(
databaseName:"ss",
containerName:"pt",
Connection = "ConnectionNameSavedInLocal.Setting.json"
)]
List<todo> todos,
FunctionContext executionContext){}
Host.json
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
},
"extensions": {
"cosmosDB": {
"connectionMode": "Direct",
"userAgentSuffix": "MyDesiredUserAgentStamp"
}
}
}
}
local.setting.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolate
d",
"CosmosConnection": "AccountEndpoint=https: <ConnectionString>"
}
}
In portal I checked the tab Connection String and it looks like this
[
{
"name": "CosmosConnection",
"value": "AccountEndpoint=
<ConnectionString>",
"type": "DocDb",
"slotSetting": false
}
]
key must be same in Local and portal appsettings.and check the binding in integration.local.settings.json:
I have published the above function into azure portal successfully. check below:
Check the binding of cosmosdb connection like below: