static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.UseWindowsService()
.UseNServiceBus(ctx =>
{
var endpointConfiguration = new EndpointConfiguration("bindingName");
var containerSettings = endpointConfiguration.UseContainer(new WindsorServiceProviderFactory());
containerSettings.ConfigureContainer(c => ConfigureCastleWindsor(c));
endpointConfiguration.SendFailedMessagesTo("error");
endpointConfiguration.AuditProcessedMessagesTo("audit");
endpointConfiguration.EnableInstallers();
return endpointConfiguration;
});
}
private static IWindsorContainer ConfigureCastleWindsor(IWindsorContainer c)
{
return c.Register(Component.For<DbStore>()
.DependsOn(Dependency.OnValue("Connection", ConfigurationManager.ConnectionStrings["name"].ConnectionString))
.LifeStyle.PerThread,
Component.For<IManageUnitsOfWork>().ImplementedBy<LinqUnitOfWork>().LifestyleTransient(),
Component.For<IMapper>().UsingFactoryMethod(x => new Mapper(ContractAutoMapper.Start())).LifestyleSingleton());
}
我引用了Nservice Bus文档链接(https://docs.particular.net/nservicebus/dependency-injection/castlewindsor(来实现第三方城堡温莎作为Nservice Bus 7.2.3 + NServiceBus.Extensions.Hosting中的DI
。套餐信息 :
- Host Window Service packages - NServiceBus.Extensions.Hosting.1.0.1
- NServiceBus.Extensions.DependencyInjection (1.0.1(
- 城堡核心 (4.2.0(
- 温莎城堡 (4.1.1(
- Castle.Windsor.MsDependencyInjection (3.0.0(
- NServiceBus.CastleWindsor (7.0.0(
注意:具有内部管理模式的第三方温莎城堡
错误信息:内部管理的容器已使用"EndpointConfiguration.UseContainer"进行配置。不能同时使用内部管理的容器和外部管理的容器。
堆栈跟踪:NServiceBus.ContainerComponent.InitializeWithExternallyManagedContainer(IConfigureComponents configureComponents)
at NServiceBus.EndpointCreator.CreateWithExternallyManagedContainer(EndpointConfiguration endpointConfiguration, IConfigureComponents configureComponents)
at NServiceBus.HostBuilderExtensions.<>c__DisplayClass0_0.<UseNServiceBus>b__0(HostBuilderContext ctx, IServiceCollection serviceCollection)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at Report.Program.Main(String[] args)
我没有使用任何外部管理器模式,但仍然运行此错误,您能否让我们知道如何使用第三方温莎城堡作为 DI。
错误消息说明了问题所在。使用使用NServiceBus.Extensions.Hosting
包的自定义容器时,需要在UseNServiceBus
扩展方法之前和外部执行此操作。这是一个工作片段:
static IHostBuilder CreateHost()
{
return Host.CreateDefaultBuilder()
.UseServiceProviderFactory(context => new WindsorServiceProviderFactory())
.ConfigureContainer<WindsorContainer>((ctx, container) =>
{
container.Register(Component.For<MyService>()); // <- Windsor registrations can go here.
})
.UseNServiceBus(nsb =>
{
//Don't apply the custom container here.
var endpointConfiguration = new EndpointConfiguration("Samples.Castle");
endpointConfiguration.UseTransport<LearningTransport>();
return endpointConfiguration;
});
}