英文:
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<ApplicationDbContext>().InstancePerLifetimeScope();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
// builder.AddCheck<MyServiceHealthCheck>("Service ACheck"); <- not working
}
}
答案1
得分: 0
短答案是:你不能这样混合使用 Microsoft 的注册与 Autofac 的注册。这就是为什么有 ConfigureServices
和 ConfigureContainer
分开的原因。
更详细的答案是:如果你想深入研究 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论