How do you use a Cosmos connection string in Azure App Service?

JasonStone-1737 51 Reputation points
2024-03-06T01:59:25.7333333+00:00

Hello,

I'm hoping someone can provide guidance on how to use a Cosmos DB connection string in Azure App Service Environment Variables.

I have placed a connection string in Azure App Service - Environment Variables - Connection Strings.

A log stream from Azure App Service indicates that a call to instantiate a CosmosClient from the Microsoft.Azure.Cosmos SDK with an ASP.NET Core app has received a null value.

This implies the Environment Variable from App Service is not passing any value.

One thing that puzzles me about the Environment Variable - Connection String setup in App Service is that you are to specify a "type" for a connection string and there is no explicit type listed as "Cosmos DB". The closest is "DocumentDB".

Does anyone know how to use a Cosmos DB connection string in Azure App Service Environment Variables?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,901 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
{count} votes

Accepted answer
  1. ajkuma 28,036 Reputation points Microsoft Employee Moderator
    2024-03-08T10:32:31.7666667+00:00

    @JasonStone-1737 , Summarizing the answer from our discussion in the comments section.

    Requirement:

    The requirement is to store the connection string for Cosmos DB as an environment variable in Azure App Service for an ASP.NET Core application.

    Scenario:

    When attempting to store the Cosmos DB connection string as an environment variable in Azure App Service, specifically using the "Connection Strings" feature, the configuration retrieval process for the connection string encounters difficulties.

    Issue:

    Despite attempts to store the Cosmos DB connection string as an environment variable using the "Connection Strings" feature of Azure App Service, the retrieval of the connection string through calls from Microsoft.Extensions.Configuration methods, such as IConfiguration.GetConnectionString() or IConfiguration.GetSection(), returns null values.

    Potential Cause:

    The cause of the issue lies in the mismatch between the way Azure App Service handles "Connection Strings" and the configuration retrieval mechanism in the ASP.NET Core application using Microsoft.Extensions.Configuration. The discrepancy may be related to the specific configuration setup or version compatibility issues.

    Resolution:

    To resolve the issue, it is suggested to store the Cosmos DB connection string as an "App Setting" environment variable instead of using the "Connection Strings" feature.

    View the Azure Cosmos DB keys in application settings

    Storing the connection string as an "App Setting" allows for successful retrieval using IConfiguration.GetSection(). This approach ensures compatibility and successful configuration retrieval within the ASP.NET Core application running on Azure App Service.

    -This doc outlines the steps to leverage Connection string prefixes in ASP.NET Core.

    Additionally, review and consider updating the application to the latest .NET Core version to address any potential issues or improvements in the configuration code related to Azure App Service integration.


    If the answer helped (pointed you in the right direction) > please click Accept Answer

    1 person found this answer helpful.
    0 comments No comments

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.