如何在Autofac模块中注册HealthCheck

huangapple go评论43阅读模式
英文:

How to register HealthCheck in Autofac module

问题

I'm 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
    }
}
英文:

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&lt;ApplicationDbContext&gt;().InstancePerLifetimeScope();
            builder.RegisterType&lt;UnitOfWork&gt;().As&lt;IUnitOfWork&gt;().InstancePerLifetimeScope();

            // builder.AddCheck&lt;MyServiceHealthCheck&gt;(&quot;Service ACheck&quot;); &lt;- not working
        }
    }

答案1

得分: 0

短答案是:你不能这样混合使用 Microsoft 的注册与 Autofac 的注册。这就是为什么有 ConfigureServicesConfigureContainer 分开的原因。

更详细的答案是:如果你想深入研究 AddHealthCheck 代码,弄清它在底层是如何工作的,你可以创建自己的 Autofac 版本,它会生效。但这需要你自己来完成,而不是由 Autofac 提供支持,它只会由你来支持。

然而,从问题的意图来看,似乎你想在 Autofac 模块中进行 MS 注册,这是不受支持的。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月6日 18:29:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定