Reputation: 1
Following the MS-Docs for creating HTTP Trigger to fetch list of documents from Azure CosmosDB fails to publish every time. Below is the code and local.settings.json file respectively
I tried changing the Connection string name from "CosmosDBConnection" to some other name, yet cannot publish the same to azure.
Upvotes: 0
Views: 190
Reputation: 8167
While creating cosmosDB Function Trigger in Visual studio, There's an option to add your Collection name, Connection String setting name, and Database table name. After adding those values, In the next section, Visual studio will prompt you to add the connection string setting like below, Make sure you login to your Azure account and select the correct Cosmos DB account like below:-
Now, My Cosmos DB Trigger Function is created successfully like below:-
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace FunctionApp18
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([CosmosDBTrigger(
databaseName: "ToDoList",
collectionName: "Items",
ConnectionStringSetting = "CosmosDbconnection",
LeaseCollectionName = "leases")]IReadOnlyList<Document> input,
ILogger log)
{
if (input != null && input.Count > 0)
{
log.LogInformation("Documents modified " + input.Count);
log.LogInformation("First document Id " + input[0].Id);
}
}
}
}
As my connection string setting is stored in local secrets, My local.settings.json looks like below:-
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
}
}
Publish the function in Azure like below:-
Add the below settings to create lease collection if not exist in your function app:-
Lease collection cannot be created locally in Visual Studio, Thus you need too deploy your Function trigger and before deployment you need to add the settings below in your Azure Function app Configuration:-
"CreateLeaseContainerIfNotExists": true
Function got deployed successfully:-
An alternate way is to use Azure Portal to create Azure cosmosdb trigger refer below:-
The portal will create Lease with azure functions if it does not exist.
Reference:-
Create a function triggered by Azure Cosmos DB | Microsoft Learn
Upvotes: -1