在 NServiceBus 中检索 Autofac IContainer
Retrieve Autofac IContainer in NServiceBus
我想知道是否有办法在注册后随时检索 autofac 容器。我按如下方式注册模块(使用 NSB 5.0):
var builder = new ContainerBuilder();
builder.RegisterModule(new AutofacConfigModule());
container = builder.Build();
busConfiguration.UseContainer<AutofacBuilder>(c => c.ExistingLifetimeScope(container));
当我的端点启动时,我注册了一个侦听器(实现 IWantToRunWhenBusStartsAndStops),它从 WebSphereMQ
检索消息。为了处理这些消息,我定义了一个接口(类似于 NServiceBus
IHandleMessages<>),然后我想解析该接口并将反序列化的消息传递给它以进行进一步处理。为了解决我的自定义接口实现,我想使用我已经在 NServiceBus
注册的容器,但我不知道该怎么做。有什么办法找回吗?
是否可以使用 ContainerDefinition
注册您自己的 Autofac 中间件实现?
http://docs.particular.net/nservicebus/containers/
您可以注册 Autofac 并将根 ILifetimeScope
存储为静态 属性 以在您的应用程序中使用。
类似于:
public static class Container
{
public static ILifetimeScope LifetimeScope { get; set; }
}
public class MyContainer : ContainerDefinition
{
public override IContainer CreateContainer(ReadOnlySettings settings)
{
var builder = new ContainerBuilder();
builder.RegisterModule(new AutofacConfigModule());
Container.LifetimeScope = builder.Build();
return Container.LifetimeScope;
}
}
然后使用
解决您的依赖关系
Container.LifetimeScope.Resolve<IHandleDeserializedMessages>();
(完全未经测试的示例代码。)
如果您在容器中注册了您的接口,您可以使用构造函数注入来解析它们。考虑这样的事情:
var cfg = new BusConfiguration();
var builder = new ContainerBuilder();
builder.RegisterInstance<IMyCustomHandler>(new MyCustomHandler());
var container = builder.Build();
cfg.UseContainer<AutofacBuilder>(c => c.ExistingLifetimeScope(container));
然后,你可以通过构造函数参数注入IMyCustomHandler
,它应该由NServiceBus自动解析。
这有帮助吗?
我想知道是否有办法在注册后随时检索 autofac 容器。我按如下方式注册模块(使用 NSB 5.0):
var builder = new ContainerBuilder();
builder.RegisterModule(new AutofacConfigModule());
container = builder.Build();
busConfiguration.UseContainer<AutofacBuilder>(c => c.ExistingLifetimeScope(container));
当我的端点启动时,我注册了一个侦听器(实现 IWantToRunWhenBusStartsAndStops),它从 WebSphereMQ
检索消息。为了处理这些消息,我定义了一个接口(类似于 NServiceBus
IHandleMessages<>),然后我想解析该接口并将反序列化的消息传递给它以进行进一步处理。为了解决我的自定义接口实现,我想使用我已经在 NServiceBus
注册的容器,但我不知道该怎么做。有什么办法找回吗?
是否可以使用 ContainerDefinition
注册您自己的 Autofac 中间件实现?
http://docs.particular.net/nservicebus/containers/
您可以注册 Autofac 并将根 ILifetimeScope
存储为静态 属性 以在您的应用程序中使用。
类似于:
public static class Container
{
public static ILifetimeScope LifetimeScope { get; set; }
}
public class MyContainer : ContainerDefinition
{
public override IContainer CreateContainer(ReadOnlySettings settings)
{
var builder = new ContainerBuilder();
builder.RegisterModule(new AutofacConfigModule());
Container.LifetimeScope = builder.Build();
return Container.LifetimeScope;
}
}
然后使用
解决您的依赖关系Container.LifetimeScope.Resolve<IHandleDeserializedMessages>();
(完全未经测试的示例代码。)
如果您在容器中注册了您的接口,您可以使用构造函数注入来解析它们。考虑这样的事情:
var cfg = new BusConfiguration();
var builder = new ContainerBuilder();
builder.RegisterInstance<IMyCustomHandler>(new MyCustomHandler());
var container = builder.Build();
cfg.UseContainer<AutofacBuilder>(c => c.ExistingLifetimeScope(container));
然后,你可以通过构造函数参数注入IMyCustomHandler
,它应该由NServiceBus自动解析。
这有帮助吗?