akhil
akhil

Reputation: 1883

Azure web app is not fetching values from configuration Tab

I developed an asp.net core API that saves images to the Azure blob storage account and inserts blob URI in cosmos DB.

In my local, it is working as expected. (I am reading storage account and cosmos Db connection strings from appsetting.json in my local).

I deployed my service in the Azure web app and this time I want to read those connections from the azure web app configuration(Settings --> Configuration Tab) instead of reading from appsettings.json. So I provided the configuration in azureweb app itself as mentioned in the screenshot.

enter image description here

But when I call my endpoint it is showing the following error (Saying my connection string is null)

System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString')
   at Azure.Storage.StorageConnectionString.Parse(String connectionString)
   at Azure.Storage.Blobs.BlobServiceClient..ctor(String connectionString, BlobClientOptions options)
   at Azure.Storage.Blobs.BlobServiceClient..ctor(String connectionString)
   at BikesIsland.API.Startup.<>c__DisplayClass4_0.<ConfigureServices>b__0(IServiceProvider factory) in D:\a\BikesIsland\BikesIsland\BikesIsland.API\Startup.cs:line 64

Here is my appsetting.json structure

{
  "AllowedHosts": "*",
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": ""
  },
  "CosmosDbSettings": {
    "ConnectionString": "",
    "DatabaseName": "bikes-island",
    "BikeContainerName": "Bikes",
    "EnquiryContainerName": "Enquiry",
    "BikeReservationContainerName": "bike-reservation",
    "BikePartitionKeyPath": "/id",
    "EnquiryPartitionKeyPath": "/id",
    "BikeReservationPartitionKeyPath": "/bikeId"

  },
  "BlobStorageSettings": {
    "ConnectionString": "",
    "ContainerName": "bikesimages"
  },
  "ServiceBusSettings": {
    "ConnectionString": "",
    "QueueName": "reservation"
  }

}

Please help me to resolve the above error. As I am completely new to the azure web app.

Thanks in advance

Upvotes: 3

Views: 808

Answers (2)

Harshita Singh
Harshita Singh

Reputation: 4870

This is because you are reading value of BlobStorageSettings:ConnectionString here in your code:

var value = Configuration.GetSection("BlobStorageSettings:ConnectionString").Value;

While you have added BlobStorageSettings__ConnectionString in your App Settings. please change that to BlobStorageSettings:ConnectionString and it shall work.

Upvotes: 1

akhil
akhil

Reputation: 1883

Adding to @singhh-msft suggested I also updated the startup.cs file

Here is the following code snippet I updated in the startup constructor

var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddEnvironmentVariables()
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();

and I updated the configuration tab in settings as @singhh-msft suggested

Pls refer below screenshot enter image description here

After these changes, it started working to me

Upvotes: 0

Related Questions