1

I have the following code in my Program.cs:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)            
        .ConfigureAppConfiguration(b => b
            .AddEnvironmentVariables()
            .AddMyCustomConfiguration(o => o.UseSqlServer(
                Environment.GetEnvironmentVariable("ConnectionStrings:MyDatabase"))))                
        .UseStartup<Startup>();

My appsettings.json looks like this:

{
  "ConnectionStrings": {
    "MyDatabase": "connectionstring"
  }
}

However, Environment.GetEnvironmentVariable("ConnectionStrings:MyDatabase") is returning null. I was under the impression that AddEnvironmentVariables would load the necassary variables. Is this not the case / how can I get this to load the connection string?

3 Answers 3

1

For getting ConnectionStrings from appsettings.json, you should use GetConnectionString instead of reading environment.

Try something like below:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)            
    .ConfigureAppConfiguration(b => {
            b.AddEnvironmentVariables();
            var connectionString = b.Build().GetConnectionString("MyDatabase");
            .AddMyCustomConfiguration(o => o.UseSqlServer(connectionString));
    })              
    .UseStartup<Startup>();
Sign up to request clarification or add additional context in comments.

3 Comments

This still returns null
@CuriousDev Is there any demo to reproduce your issue?
Thank you for this @Edward, It works!!!! I think OP didn't realize .Build() part of it
0

See this example

 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
  .ConfigureAppConfiguration((hostingContext, config) =>
  {
    var env = hostingContext.HostingEnvironment;

    config.AddJsonFile("application.json", optional: false, reloadOnChange: true);
    config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
    config.AddEnvironmentVariables("ASPNETCORE_");
    config.AddCommandLine(args);
  })
  .UseStartup<Startup>();

here you have the link to the article https://www.johanohlin.com/blog/configuration-providers-in-aspnet-core/

1 Comment

This doesn't make any difference. I've tried various combinations on the approach, but no luck. Obviously AddEnvironmentVariables("ASPNETCORE_") is no good to me, so I just load them all
0

You've put your settings into JSON config, not into env var(s). This is why GetEnvironmentVariable() returns nothing. And ASP.Net Core is not intended to export your config to env vars unless you did that explicitly (let say, via SetEnvironmentVariable() or an equivalent).

If you actually intend to just read a connection string value, take a look on this topc How to read connection string in .NET Core?

Comments

Your Answer

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