I am using NServiceBus 7. My host is trying to configure 2 endpoints for NServiceBus, but only the first endpoint ever seems to work. My reason for having two endpoints is that I need to use 2 different transport transaction modes.
When I review the configuration.txt files in the bin folder, it is only the first endpoint that has any messagehandlers listed against it.
If I swap the two code blocks around, whichever block comes first, that's the one that works. The second never does.
A previous StackOverflow question asked something similar, and the respondent posted a link to sample pages on Particular's website (which supposedly shows exactly what I'm looking for), but that link no longer takes me to an example solution :(
// Configure first endpoint - this one works fine
var endpointConfiguration1 = new EndpointConfiguration(endpointName: Queue1);
endpointConfiguration1.SendFailedMessagesTo("error");
endpointConfiguration1.UseSerialization<JsonSerializer>();
endpointConfiguration1.EnableInstallers();
endpointConfiguration1.UsePersistence<InMemoryPersistence>();
_endpointInstance1 = await Endpoint.Start(endpointConfiguration1).ConfigureAwait(false);
// Configure second endpoint - this one never identifies any handlers
var endpointConfiguration2 = new EndpointConfiguration(endpointName: Queue2);
endpointConfiguration2.SendFailedMessagesTo("error");
endpointConfiguration2.UseSerialization<JsonSerializer>();
endpointConfiguration2.EnableInstallers();
endpointConfiguration2.UsePersistence<InMemoryPersistence>();
_endpointInstance2 = await Endpoint.Start(endpointConfiguration2).ConfigureAwait(false);
As mentioned here by Microsoft MVP Daniel Marbach the NServiceBus.Host is basically meant to host one single endpoint:
As a general guidance unless some deployement or runtime/environment restrictions required you to host multiple endpoints together we would recommend to have a single endpoint per process. As soon as you start co-hosting multiple endpoints together the configuration can become quite tricky because you have to make sure that assembly scanning does pick up the right handlers and infrastructure. Having a multi transport system is something that is not officially supported. You would need to use the community package called router NServiceBus Router and rely on the community to update the package should you have any issues with that.
You would typically have two separate projects or executables, each with its own Program.cs (or hosting model) and each one starting a single endpoint. For example:
// Program.cs
var endpointConfiguration1 = new EndpointConfiguration(endpointName: Queue1);
endpointConfiguration1.SendFailedMessagesTo("error");
endpointConfiguration1.UseSerialization<JsonSerializer>();
endpointConfiguration1.EnableInstallers();
endpointConfiguration1.UsePersistence<InMemoryPersistence>();
_endpointInstance1 = await Endpoint.Start(endpointConfiguration1).ConfigureAwait(false);
// Program.cs
var endpointConfiguration2 = new EndpointConfiguration(endpointName: Queue2);
endpointConfiguration2.SendFailedMessagesTo("error");
endpointConfiguration2.UseSerialization<JsonSerializer>();
endpointConfiguration2.EnableInstallers();
endpointConfiguration2.UsePersistence<InMemoryPersistence>();
_endpointInstance2 = await Endpoint.Start(endpointConfiguration2).ConfigureAwait(false);
you can do it using scanner.ExcludeAssemblies()
like this:
// Endpoint 1
var endpointConfiguration1 = new EndpointConfiguration("Queue1");
endpointConfiguration1.UsePersistence<InMemoryPersistence>();
endpointConfiguration1.UseTransport<LearningTransport>() // or whichever transport
.Transactions(TransportTransactionMode.SomeMode);
var scanner1 = endpointConfiguration1.AssemblyScanner();
scanner1.ExcludeAssemblies("MyCompany.Endpoint1.Handlers.dll");
// Start endpoint 1
var endpointInstance1 = await Endpoint.Start(endpointConfiguration1);
// Endpoint 2
var endpointConfiguration2 = new EndpointConfiguration("Queue2");
endpointConfiguration2.UsePersistence<InMemoryPersistence>();
endpointConfiguration2.UseTransport<LearningTransport>()
.Transactions(TransportTransactionMode.AnotherMode);
var scanner2 = endpointConfiguration2.AssemblyScanner();
scanner2.ExcludeAssemblies("MyCompany.Endpoint1.Handlers.dll");
// Start endpoint 2
var endpointInstance2 = await Endpoint.Start(endpointConfiguration2);
Caution: This approach can get brittle because you have to carefully manage the scanning rules to ensure the right endpoint picks up the right message handlers. Plus, you’ll still need to verify that the two different transport transaction modes don’t conflict in the same process.
But be aware that running multiple endpoints inside the same process can be tricky to manage and debug. If a single assembly accidentally contains both sets of message handlers (or a shared handler that’s scanned by both endpoints), you may still get collisions or unexpected behavior.