HostContext.AppSettings always null in self-hosted app

For integration testing, I have created a self-hosted app as shown below, which is the equivalent of the real app.

In the self-hosted app, the HostContext.AppSettings is always null.

public class TestAppHost : AppSelfHostBase
{
  public TestAppHost()
      : base("TESTAPP", typeof(MyService).Assembly)
  {
  }
  
  public override void Configure(Container container)
  {
    var appSettings = HostContext.AppSettings;    // fails: always null
  }
}

The equivalent apphost code works correctly under IIS Express (Rider/VisualStudio) and IIS:

public class AppHost : AppHostBase
{
  public AppHost()
      : base("APP", typeof(MyService).Assembly)
  {
  }
  
  public override void Configure(Container container)
  {
    var appSettings = HostContext.AppSettings;    // success
  }
}

I must not be understanding something, so all help appreciated.

Is this a .NET Framework App and do you have an App.config?

Sorry, Net Core, using appsettings.json.

Using latest ServiceStack 5.11, under Windows.

You should only use the AppHost singleton (i.e. HostContext) after it’s initialized, prior to that (or within your AppHost) you should just access its instance properties instead, e.g:

public override void Configure(Container container)
{
    var appSettings = AppSettings;
}

AppSelfHostBase for .NET Core requires the AppHost is started (i.e. both .Init() and .Start()) before the AppHost is properly initialized.

I tried using AppSettings in AppHost’s Configure(), as you showed, but is also not working:

public override void Configure(Container container)
{
    var connStr = AppSettings.GetString("ConnectionString");   // is null
}

The Apphost is being started using:

public class IntegrationTester : IDisposable
{
    private const string BaseUri = "http://localhost:8888";
    private readonly ServiceStackHost _appHost;
    
    public IntegrationTester()
    {
        _appHost = new AppHost()
            .Init()
            .Start(BaseUri);
    }
}

So, should the AppSettings be available in AppHost’s Configure() or is Configure() part of the Start process?

Is this the difference to using “AppHost : AppHostBase” when using IIS/IISExpress?

Typically AppSelfHostBase is only used for creating multi-platform (.NET Framework/.NET Core) integration tests as such the default AppSettings looks at the same Web.config/App.config <appSettings/> as .NET Framework integration test projects.

You could override it to look at a custom ASP .NET Configuration provider using their ConfigurationBuilder, e.g:

class AppHost : AppSelfHostBase
{
    public AppHost() : base(nameof(MyTests), typeof(MyTests).Assembly)
    {
        Configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();
        AppSettings = new NetCoreAppSettings(Configuration);
    }

    public override void Configure(Container container)
    {
        var stringValue = AppSettings.GetString(stringKey);
        var intValue = AppSettings.Get<int>(intKey);
    }
}

Although if you only need to target .NET Core you may want to consider using the standard Program/Startup.cs for ASP .NET Core projects directly (which AppSelfHostBase is just wrapping), to get full configurability of Kestrel SelfHost Server.

Your last suggestion worked perfectly for self-hosting.

Thank you.