public class ConfigureCache : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder
.ConfigureServices(services =>
{
services.AddSingleton<ICacheClient, OrmLiteCacheClient>();
}).ConfigureAppHost((app) =>
{
var cache = app.Resolve<ICacheClient>();
cache.InitSchema();
});
}
}
But it seems the public properties don’t get injected so it fails with null reference exception.
I tried a workaround
public class ConfigureCache : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder
.ConfigureServices(services =>
{
services.AddSingleton<ICacheClient, CogitoCacheClient>();
}).Configure((context,app) =>
{
var cache = app.ApplicationServices.GetService<ICacheClient>();
cache.InitSchema();
});
}
}
public class CogitoCacheClient : OrmLiteCacheClient
{
public CogitoCacheClient(IDbConnectionFactory factory)
{
DbFactory = factory;
}
}
The project starts without any errors but nothing works. /locode, /ui and api endpoints give 404 but there are no errors output which is a bit confusing.
Right, it doesn’t inject public properties so you can’t use .NET’s default DI behavior and will need to provide your own constructor function, e.g:
services.AddSingleton<ICacheClient>(c => new OrmLiteCacheClient {
DbFactory = c.GetRequiredService<IDbConnectionFactory>()
});
info: Microsoft.AspNetCore.Hosting.Diagnostics[16]
Request reached the end of the middleware pipeline without being handled by application code. Request path: GET https://localhost:5001/locode, Response status code: 404
It’s because you’re registering a IWebHostBuilder.Configure() which overrides the App Startup configuration, which you basically never want to do in a HostingStartup configuration class, use ConfigureAppHost() instead: