Home > Mobile >  Does Visual Studio Publish to App Service feature deploys items in appsettings.json to the configura
Does Visual Studio Publish to App Service feature deploys items in appsettings.json to the configura

Time:11-13

I am using Visual Studio 2022 to build an asp.net core v6 application.

I need to deploy and run my application on an Azure Ass Services instance.

I have many settings in my application's appsettings.json file. The file is included in the deployment package as shown here:

enter image description here

I use Visual Studio 2022 Publish feature to deploy my asp.net core application to an Azure App Services instance:

enter image description here

After a successful Visual Studio Publish, none of appsettings.json file settings show in the configuration blade:

enter image description here

Questions:

1- Is there any way Visual Studio publish feature deploy appsettings.json settings to the App Services' configuration blade?

2- Even after a successful deployment, my application does not read the settings in the appsettings.json. It seems that all ignored. I checked and on my local visual studio developer environment, my app reads appsettings.json, but not when it is deployed to App Services. Why the deployed app ignores the appsettings.json settings?

Update 1: program.cs and appsettings.json

public static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);

    builder.Logging.ClearProviders();
    builder.Logging.AddConsole();
    builder.Logging.AddAzureWebAppDiagnostics();

    // Add services to the container.
    builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));

    builder.Services.AddControllers();
    // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();

    builder.Services.AddApplicationInsightsTelemetry();
    builder.Services.AddScoped<Framework>();
        

    var app = builder.Build();

    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
    }

    app.UseHttpsRedirection();

    app.UseAuthentication();
    app.UseAuthorization();


    app.MapControllers();

    app.Run();
}

appsettings.json could be anything. Just need to read its content with a ConfigurationManager instance like how I read them when the site is running locally in Visual Studio.

CodePudding user response:

  1. Is there any way Visual Studio publish feature deploy appsettings.json` settings to the App Services' configuration blade?

The deployed appsettings.json file will be available in KUDU Debug Console.

In your Azure App Service => Advanced Tools => Go => Debug Console => cmd => site => wwwroot.

enter image description here

  • Or even you can open the KUDU with the below URL
https://YourAppServiceName.scm.azurewebsites.net/
  • Configuration section will not display the setting which are in appsettings.json file. It is used to override the available settings from appsettings.jsonfile.

  • The values which you want to override and the new values which you want to add after deployment must be set in the Application Settings in portal.

  • The key name must be same in both appsettings.json and Configuration => Application Settings.

enter image description here

My sample appsettings.json :

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "MyVal": "Local Connection"
  },
  "AppSettings": {
    "MySettings": "Local Settings"
  }
}

Code to retrieve values from appsettings.json

var MyAppSettings = builder.Configuration.GetSection("AppSettings");
var LocalAppset = MyAppSettings.GetValue<string>("MySettings");

OR

var MyAppSettings1 = builder.Configuration.GetValue<string>("AppSettings:MySettings");

Index.cshtml

@page
@model IndexModel
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration

<h2>Configuration value for 'AppSettings': @Configuration["AppSettings:MySettings"]</h2>
  • I have deployed the app and able to read the values from the deployed code.

Initial Values enter image description here

Overridden Values enter image description here

  • Related