Overriding appsettings.json in IIS

Viewed 2860

I have a .net core web project which I'm publishing to IIS on Windows (10 or Server). I cannot seem to get IIS to provide the connection string through the Configuration Editor. I want the connection strings here so that it's not overwritten after every deploy.

I have tried using ASPNETCORE_ConnectionStrings__DefaultConnection and ConnectionStrings:DefaultConnection but neither do the job I always get a 500 error stating that the connection string cannot be null.

What am I missing here?

Here's part of my Startup.cs file: Note that this application was in a time before the default config did this for you.

 public class Startup
 {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddUserSecrets<Startup>()
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }
}

Edit:

I've setup a brand new .Net Core 2.2 app. Even with the newer program/startup. The connection string doesn't come through.

public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args) //<-- adds config.AddEnvironmentVariables();
                .UseStartup<Startup>();
    }

enter image description here

1 Answers

I think you need to remove the ASPNETCORE_ prefix from your Environment variable (Just your connection string not the ASPNETCORE_ENVIRONMENT).

I'm looking at the Microsoft source code here for the Environment Variables Configuration Provider : https://github.com/aspnet/Configuration/blob/d469707ab18eef7ed0002f00175a9ad5b0f36250/src/Config.EnvironmentVariables/EnvironmentVariablesConfigurationProvider.cs

And it does take a "prefix" if you want it do, but when I look at the source code for "CreateDefaultBuilder" : https://github.com/aspnet/MetaPackages/blob/master/src/Microsoft.AspNetCore/WebHost.cs#L177

It just calls it with an empty prefix param, meaning that you shouldn't use any prefix at all :

config.AddEnvironmentVariables();

You can ofcourse manually add the call to AddEnvironmentVariables with a prefix of your choosing that you can then prepend to your environment variables too

Related