我知道您可以向 LifetimeScope
添加注册 当它是这样创建的:
using(var scope = container.BeginLifetimeScope(builder =>
{ builder.RegisterType<Override>().As<IService>();
builder.RegisterModule<MyModule>();
}))
{
// The additional registrations will be available
// only in this lifetime scope.
}
LifetimeScope
创建后是否可以添加注册? (例如在 using
block 中添加注册)
最佳答案
您可以访问 ILifetimeScope
的 ComponentRegistry
属性,使用此对象可以注册新的 IComponentRegistration
,您可以使用 创建它RegistrationBuilder
静态方法:
using(var scope = container.BeginLifetimeScope())
{
var registration = RegistrationBuilder.ForType<ServiceA>()
.As<IService>()
.CreateRegistration();
scope.ComponentRegistry.Register(registration);
scope.Resolve<IService>();
}
关于c# - 是否可以在创建 Autofac LifetimeScope 后向其添加注册?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38877510/