Search code examples
.net-6.0autofacautofac-module

How to register HealthCheck in Autofac module


Im using .NET 6 and Autofac to register my dependencies, all works great. However I was wondering how can I register a healthcheck in my module (not in the startup.cs), ex:

public class InfrastructureModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<ApplicationDbContext>().InstancePerLifetimeScope();
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();

            // builder.AddCheck<MyServiceHealthCheck>("Service ACheck"); <- not working
        }
    }

Solution

  • Short answer is: you can't. You can't mix and match Microsoft registrations with Autofac registrations like that. That's why there's ConfigureServices separate from ConfigureContainer.

    Longer answer is: if you want to go spelunking through the code of AddHealthCheck and figure out what it's doing under the covers, you could make your own Autofac version of that and it would work. But it's work for you to do, not something Autofac provides, and it'd be supported only by you.

    However, reading the intent of the question, it sounds like you want to do MS registrations in an Autofac module and that's not a supported thing.