5

In older versions of ASP.Net core you had the startup.cs file where you would do a lot of the work including reading and setting the application environment then based on that you could read different version of the appsettings.json file. In the new ASP.Net Core 7 they got rid of Startup.cs and greatly streamlined the program.cs files. Now I can't figure out how to read the environment and then pass Entity Framework 7 my connection string. Looking around all the answers I find don't apply to version 7 or tell you to undo all the work in 7 by remaking the entire Startup.cs file. How are we supposed to inject the connection string based off the environment in .Net 7?

I do have the code to read from the base appsettings.json file and that works, there is also a section to read the environment but it get setup AFTER the DbContext injection. Here is my program.cs file and I am just lost on what needs to be updated. I looked at the microsoft docs but did not see anything applying to environements and injection of the string.

var builder = WebApplication.CreateBuilder(args);

//Add Services (builder.Services.AddScoped<IService, Service>();
builder.Services.AddScoped<INavigationHelper, NavigationHelper>();
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme).AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));

builder.Services.AddAuthorization(options =>
{
    options.FallbackPolicy = options.DefaultPolicy;
});

builder.Services.AddControllersWithViews(options =>
{
    var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
    options.Filters.Add(new AuthorizeFilter(policy));
});

builder.Services.AddRazorPages().AddMicrosoftIdentityUI();
builder.Services.AddDbContext<SiteDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("Database")));
var app = builder.Build();

if (!app.Environment.IsDevelopment()) { app.UseHsts(); }

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers();
app.Run();

appsettings.json:

{
  "AzureAd":
  {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "",
    "TenantId": "",
    "ClientId": "",
    "CallbackPath": "",
    "ClientSecret": "Client secret from app-registration. Check user secrets/azure portal.",
    "ClientCertificates":
    [
    ]
  },
  "Logging":
  {
    "LogLevel":
    {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",

  "ConnectionStrings":
  {
    "Database": ""
  }
}

appsettings.development.json

{
  "Logging":
  {
    "LogLevel":
    {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    },

    "ConnectionStrings":
    {
      "Database": ""
    }
  }
}

2 Answers 2

5
builder.Configuration.GetConnectionString("Database")

will use the appropriate appsettings.json file based on the chosen environment. If you look in Properties folder in your project and examine the launchsettings.json file you should see something like this:

{
    "iisSettings": {
      "windowsAuthentication": false,
      "anonymousAuthentication": true,
      "iisExpress": {
        "applicationUrl": "http://localhost:30787",
        "sslPort": 44345
      }
    },
    "profiles": {
      "Cosmos.Application.Server": {
        "commandName": "Project",
        "dotnetRunMessages": true,
        "launchBrowser": false,
        "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
        "applicationUrl": "https://localhost:7138;http://localhost:5138",
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        }
      },
      "IIS Express": {
        "commandName": "IISExpress",
        "launchBrowser": true,
        "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        }
      }
    }
  }

In the above, this means that the ASPNETCORE_ENVIRONMENT is set to Development and this will determine which appsettings file is used in your program.cs when calling:

builder.Configuration ...
Sign up to request clarification or add additional context in comments.

6 Comments

So in do have "ASPNETCORE_ENVIRONMENT": "Development". But if I put my connectionstring in appsettings.Development.json it does not get read, but if I copy/paste it to appsettings.json it is read just fine.
Are you sure there's no typo in your Development file? It will revert to base appsettings.json file for values if the requested value cannot be found in the environment-specific file. Can you post your appsettings.Development.json file (hiding sensitive data)?
I don't see one but will update post with the code from both json files for review.
Your "ConnectionStrings" is nested in logging. It needs to be a root section.
Ok, I freaking hate my self. I have spent hours trying to figure this out and never noticed I screwed that up. Thank you for seeing it.
|
1

We can read configs from environments like below. Keys within latest added file will get the priority when reading the values.

For example if "Default" connection string was present in both appsettings.json and appsettings.Development.json, then the value in latter file will be the resulting value.

using Microsoft.Extensions.Configuration;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

 var config = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
            .Build();

var defaultConnectionString = config.GetConnectionString("Default");


Console.WriteLine($"Default ConnectionString: {defaultConnectionString}");

5 Comments

How would your code know which environment I am running in and pull from the right JSON file? Say I have a development,json and a production.json file I need the right file read for the right environment. Also I am 99% sure the code you provided to add the files is automatically done by EF 7.
You can do it like below, var config = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true) .Build();
Have updated the code to reflect that @MatthewVerstraete
Reading over learn.microsoft.com/en-us/aspnet/core/fundamentals/… it looks to me that EF already does what your wanting me to do. So the problem remains that it is not reading from the right appsetting.{Environment}.json file it appears
Yep. Thanks for pointing out. I concur with Neil's comment on ConnectionString setting value not being in the correct hierarchy within development configuration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.