NService Bus throwing an Exception While Using Castle Windsor

I am referring Nservice Bus document link (Castle Windsor • Castle Dependency Injection • Particular Docs) to implement third party castle windsor as DI in Nservice Bus 7.2.3 + NServiceBus.Extensions.Hosting .

Packages Information :

• Host Window Service packages - NServiceBus.Extensions.Hosting.1.0.1
• NServiceBus.Extensions.DependencyInjection (1.0.1)
• Castle.Core (4.2.0)
• Castle.Windsor (4.1.1)
• Castle.Windsor.MsDependencyInjection (3.0.0)
• NServiceBus.CastleWindsor (7.0.0)

NOTE : Third party Castle Windsor with internal managed mode

Binding Code Window Service :

static IHostBuilder CreateHostBuilder(string args)
{
return Host.CreateDefaultBuilder(args)
.UseWindowsService()
.UseNServiceBus(ctx =>
{
var endpointConfiguration = new EndpointConfiguration(“bindingName”);

// binding related code goes here

           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());
   }

Error Information: An internally managed container has already been configured using ‘EndpointConfiguration.UseContainer’. It is not possible to use both an internally managed container and an externally managed container.

Stack Trace : at NServiceBus.ContainerComponent.InitializeWithExternallyManagedContainer(IConfigureComponents configureComponents)
at NServiceBus.EndpointCreator.CreateWithExternallyManagedContainer(EndpointConfiguration endpointConfiguration, IConfigureComponents configureComponents)
at NServiceBus.HostBuilderExtensions.<>c__DisplayClass0_0.b__0(HostBuilderContext ctx, IServiceCollection serviceCollection)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at Program.Main(String args)

I am not using any external manager mode but still, I am running with this error, could you please let us know how do we use third-party Castle Windsor as DI.

When using NServiceBus.Extensions.Hosting you should not use NServiceBus.Extensions.DependencyInjection since the hosting package will automatically make sure the configured container is used.

To configure the container you would do the Windsor equivalent to what AutoFac provides here:

In short:

  1. Remove the reference to NServiceBus.Extensions.DependencyInjection
  2. Remove the call to endpointConfiguration.UseContainer(new WindsorServiceProviderFactory())
  3. Configure Windsor according to Support for .net core generic hosts · Issue #412 · castleproject/Windsor · GitHub

Hope this helps!