Harshal Bhoir
Harshal Bhoir

Reputation: 1

Using Azure HTTP trigger to get a list of documents from CosmosDB fails every time

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

Azure Function Code

local.settings.json File

I tried changing the Connection string name from "CosmosDBConnection" to some other name, yet cannot publish the same to azure.

Upvotes: 0

Views: 190

Answers (1)

SiddheshDesai
SiddheshDesai

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:-

enter image description here

enter image description here

enter image description here

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:-

enter image description here

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

enter image description here

Function got deployed successfully:-

enter image description here

enter image description here

An alternate way is to use Azure Portal to create Azure cosmosdb trigger refer below:-

enter image description here

enter image description here

enter image description here

The portal will create Lease with azure functions if it does not exist.

enter image description here

Reference:-

Create a function triggered by Azure Cosmos DB | Microsoft Learn

Upvotes: -1

Related Questions