Reputation: 569
I have a Web API application working with StructureMap and I've introduced the latest version of NServiceBus (version 6) to introduce pub/sub for changed data.
The problem is I don't seem to be able to get the existing container to inject to the StructureMapBuilder.
The structure is as follows:
public class WebApiRegistry : Registry
{
public WebApiRegistry()
{
For(typeof(IRepository<>)).Use(typeof(Repository<>));
For(typeof(IProcessor<>)).Use(typeof(PassThroughProcessor<>));
For<IUnitOfWork>().Use<UnitOfWork>();
For<ILog>().Use(container => LogManager.GetLogger("My.WebApi"));
}
}
This registry is then registered in the Aplication_Start method in the Global.asax:
GlobalConfiguration.Configuration.UseStructureMap<WebApiRegistry>();
The problem is here, in the same method:
var endpointConfiguration = new EndpointConfiguration("My.WebApi.Sender");
endpointConfiguration.UseTransport<MsmqTransport>();
endpointConfiguration.UseSerialization<NServiceBus.JsonSerializer>();
endpointConfiguration.UsePersistence<InMemoryPersistence>();
endpointConfiguration.UseContainer<StructureMapBuilder>(); //No idea how to get the existing container in here???
endpointConfiguration.SendOnly();
I cannot figure out how to register the existing container with NServiceBus.
I've got an example working with AutoFac which is probably because it's the default DI framework of choice for NServiceBus, but I'm keen to get it working with StructureMap.
Any ideas?
Upvotes: 1
Views: 876
Reputation: 247078
Create the container manually and use it for both Web API and NServiceBus
var registry = new WebApiRegistry();
var container = new Container(registry);
//Register StructureMap with GlobalConfiguration
GlobalConfiguration.Configuration.UseStructureMap(container);
var endpointConfiguration = new EndpointConfiguration("My.WebApi.Sender");
//...other code removed for brevity
//Configuring NServiceBus to use the container
endpointConfiguration.UseContainer<StructureMapBuilder>(
customizations: customizations => {
customizations.ExistingContainer(container);
});
//...other code removed for brevity
var endpointInstance = await Endpoint.Start(endpointConfiguration);
// OR Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
IMessageSession messageSession = endpointInstance as IMessageSession;
// Now, change the container configuration to know how to resolve IMessageSession
container.Configure(x => x.For<IMessageSession>().Use(messageSession));
Upvotes: 2