0

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.

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • 1
    Add code as text, not image or external link. You can format it as code, using three backticks on a separate line. => [help: formatting](https://stackoverflow.com/help/formatting) – Fildor Jul 06 '23 at 12:38
  • Which docs did you follow? I believe it needs to be `ConnectionStringSetting` instead of `Connection` => [Azure Cosmos DB input binding for Azure Functions 2.x and higher # Attributes](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-input?tabs=python-v2%2Cin-process%2Cfunctionsv2&pivots=programming-language-csharp#attributes) – Fildor Jul 06 '23 at 12:57
  • 2
    and what is the error? – Thiago Custodio Jul 06 '23 at 13:23

1 Answers1

-1

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

SiddheshDesai
  • 3,668
  • 1
  • 2
  • 11
  • The question has nothing to do with visual studio or the portal. And the OP hasn’t even shown details of their error (as someone has already mentioned in comments). Also please use formatted text, not images. – David Makogon Jul 07 '23 at 12:40
  • @DavidMakogon Forgive me if i am wrong, In the last line, Op has mentioned -I tried changing the Connection string name from "CosmosDBConnection" to some other name, yet cannot publish the same to azure. I took this into consideration that Op is having issues with setting CosmosDBConnection while publishing the cosmosdb trigger, Thus my 3rd image shows the connection string setting for cosmosdb trigger? this I have given the steps to make the cosmosdb trigger work in portal and in Visual studio.. – SiddheshDesai Jul 07 '23 at 12:46
  • @DavidMakogon Also, Op has mentioned this line - fetch list of documents from Azure CosmosDB fails to publish every time. If you see the images of OP they are clearly from Visual studio > By what I understand OP is trying to publish the trigger but it is failing, I have given step by step instructions to make the deployment sucessfull, 1) Via visual studio 2) via Portal – SiddheshDesai Jul 07 '23 at 12:49
  • There's no need to write a custom tutorial on setting things up via portal - that's my point. And as of now, there's still no well-defined programming question from the OP. – David Makogon Jul 07 '23 at 12:53
  • @DavidMakogon Okay, Got your point. But by following the steps above in visual studio, Op might get the step he's missing. As adding cosmosdb connection string in local.settings.json is not mandatory if you just connect to your cosmos db account like my image 3. Thus, I took this consideration. Thank your for inputs. I'll consider them next time. – SiddheshDesai Jul 07 '23 at 13:01
  • Sorry I forgot to mention I am using Visual Studio for Mac which does not support options as listed by @SiddheshDesai – Harshal Bhoir Jul 10 '23 at 08:52
  • If you're using MAC then using CosmosDbConnection string in local.settings.json will work but in order to read your CosmosDbConnection, Add Iconfiguration as per this SO thread answer by AlbertK https://stackoverflow.com/questions/46714677/read-values-from-local-settings-json-in-vs-2017-azure-function-development also try to use Environment.GetEnvironmentVariable("CosmosDbConnection") in your function.cs to retrieve the connection string value from local.settings.json – SiddheshDesai Jul 10 '23 at 09:00
  • Also, The values from local.settings.json will not be published in azure function app, As the local.settings.json file is gitignored. You need to set the values of connectionstring manually in Azure Functions Configuration tab, If it does not auto populate. – SiddheshDesai Jul 10 '23 at 09:02