Error connecting Azure Function with CosmosDB

OLADOJA NIYI SAMAD 20 Reputation points
2023-06-07T15:28:27.4966667+00:00

I'm trying to output a document to Azure CosmosDB for Table with Azure function Http Trigger. I followed the sample from the documentation but I ran into error after running the sample code

Executed 'Functions.HttpTrigger1' (Failed, Id=60ea69c6-ba44-431d-ad26-f3d385dc008b, Duration=344ms) [2023-06-07T15:16:50.349Z] System.Private.CoreLib: Exception while executing function: Functions.HttpTrigger1. Microsoft.Azure.DocumentDB.Core: Value cannot be null. (Parameter 'serviceEndpoint').

Here is my local.settings.json

{

  "IsEncrypted": false,

  "Values": {

    "AzureWebJobsStorage": "",

    "FUNCTIONS_WORKER_RUNTIME": "python",

    "AzureCosmosDBConnectionString": "AZURE PRIMARY CONNECTION STRING"

  }

}
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,029 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,643 questions
0 comments No comments
{count} votes

Accepted answer
  1. navba-MSFT 24,795 Reputation points Microsoft Employee
    2023-06-08T08:09:50.83+00:00

    @OLADOJA NIYI SAMAD Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    I understand that you have an Azure Function App which is configured for the HTTP trigger with CosmosDB output binding and you are encountering the below error:

    Microsoft.Azure.DocumentDB.Core: Value cannot be null. (Parameter 'serviceEndpoint').
    Is the serviceEndpoint the connectionString to Cosmos DB? Have you set that under app settings in Azure?

    Looking at the error message it seems like this serviceEndpoint value is not set. I am suspecting it's the connectionString. So you may need to check what the serviceEndpoint is ? and where is it set in your code ?

    You can leverage the below sample which I have developed. It is configured for the HTTP trigger with CosmosDB output binding. So depending upon the HTTP Get, POST operation invoke, I am logging the request details in cosmsoDB and it is working fine.

    Function1.cs

    using System;
    using System.IO;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Logging;
    using Newtonsoft.Json;
    using System.Configuration;
    using Microsoft.WindowsAzure.Storage.Queue.Protocol;
    
    namespace OutCosmosHTTPTrigger
    {
        public static class Function1
        {
            [FunctionName("Function1")]
            public static async Task<IActionResult> Run(
                [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
                [CosmosDB(
                    databaseName: "testdb",
                    containerName: "testcontainer",
                    Connection  = "CosmosDBConnection")] IAsyncCollector<dynamic> documents,
                ILogger log)
            {
                dynamic document = new { Description = req.Body.ToString(), id = Guid.NewGuid() };
    
                await documents.AddAsync(document);
    
                log.LogInformation("C# HTTP trigger function processed a request.");
    
                return new OkResult();
            }
        }
    }
    
    

    Here is my local.settings.json:

    {
        "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "CosmosDBConnection": "AccountEndpoint=https://cosmosdb123.documents.azure.com:443/;AccountKey=XXXXXXXXXXXX;"
      }
    }
    

    Here is the output console logs after I invoked the GET and POST operation while executing my Function APP:
    User's image

    Here is the entity saved in the CosmosDB :
    User's image

    Hope this helps.

    I am sharing a few related articles here if that helps:
    https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-output?tabs=python-v2%2Cin-process%2Cfunctionsv2&pivots=programming-language-csharp
    https://learn.microsoft.com/en-us/answers/questions/196212/error-when-using-cosmosdb-in-python-function-on-az
    https://github.com/MicrosoftDocs/azure-docs/issues/33312

    **
    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.