ICacheClient with Microsoft DI

I tried to add OrmLiteCacheClient to asp.net 8

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.

What am I doing wrong?

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>()
});

I still get same issue when using that constructor. All pages/endpoints give 404 but no error in output. Removing the service fixes it.

Sorry, there was an output:

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

I guess something breaking in UseServicestack()?

Impossible to tell from here, are you saying locode works if you don’t register it?

Yes, if I don’t register the cache client then all endpoint and SS interfaces work. I register it then everything gives a 404.

Standalone repo : GitHub - GuerrillaCoder/standalone-repro

removing Configure.Cache.cs will make it work

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:

builder.ConfigureServices(services => {
    services.AddSingleton<ICacheClient>(c => new OrmLiteCacheClient
    {
        DbFactory = c.GetRequiredService<IDbConnectionFactory>()
    });
})
.ConfigureAppHost(appHost => {
    var cache = appHost.Resolve<ICacheClient>();
    cache.InitSchema();
});

1 Like

Ahh my bad, I was just looking at order of execution, I didn’t realize it completely overrode it.

Many thanks Mythz, as always I really appreciate everything you do!

1 Like