Home > Back-end >  Extra config in IHostingStartup not possible?
Extra config in IHostingStartup not possible?

Time:11-04

Using the new WebApplication in Program.cs, and adding some extra config to the builder:

var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("extra_appsettings.json");
var app = builder.Build();

I'd think that the config would be available in IHostingStartup implementations, e.g.

public void Configure(IWebHostBuilder builder) => builder
   .ConfigureServices((context, services) => {
context.Configuration.GetConnectionString("connectionStringFromOtherFile")

Anyone have a solution to this? When debugging it actually seems that all the IHostingStartup implementations are being called directly from CreateBuilder() before we have a chance to .AddJsonFile.

CodePudding user response:

You can do below to make the settings from the extra_appsettings.json file (being loaded from the web application) available to an IHostingStartup implementation in an other assembly.

In the web application project

Don't load that extra_appsettings.json file from Program.cs, so remove below line.

builder.Configuration.AddJsonFile("extra_appsettings.json");

Instead, add an IHostingStartup implementation to the web application project itself and make that one load the extra_appsettings.json file. Also set an HostingStartupAttribute.

[assembly: HostingStartup(typeof(MyWebApp.HostStartUp))]

namespace MyWebApp;

public class HostStartUp : IHostingStartup
{
    public void Configure(IWebHostBuilder builder)
        => builder.ConfigureAppConfiguration(
                (context, configurationBuilder) => configurationBuilder.AddJsonFile("extra_appsettings.json")
            );
}

In the class library project

Add an IHostingStartup implementation with matching HostingStartupAttribute (like the one you already have).

[assembly: HostingStartup(typeof(MyClassLibrary.HostStartUp))]

namespace MyClassLibrary;

public class HostStartUp : IHostingStartup
{
    public void Configure(IWebHostBuilder builder)
        => builder.ConfigureServices((ctx, services) =>
        {
            var cns = ctx.Configuration.GetConnectionString("DefaultConnection");
            // ...
        });
}

Configure the loading of the IHostingStartup implementations

The documentation shows that one way to do this is by setting the ASPNETCORE_HOSTINGSTARTUPASSEMBLIES environment variable. Note that the one in the web application does not need to be part of this configuration; it runs automatically.

Order matters if you have more than one assembly with a IHostingStartup. The documentation mentions that the order is guaranteed.

When multiple hosting startup assembles are present, their Configure methods are executed in the order that the assemblies are listed.

For above setup, the environment variables look like below.

From the launchSettings.json file.

"environmentVariables": {
  "ASPNETCORE_ENVIRONMENT": "Development",
  "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "MyClassLibrary"
}

Now the connection string will be available when making below call from within the IModuleStartup implementation in that other assembly.

var cns = ctx.Configuration.GetConnectionString("DefaultConnection");
  • Related