最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

2 NServiceBus endpoints in the same host - Stack Overflow

programmeradmin1浏览0评论

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

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);
Share Improve this question edited Feb 4 at 16:47 Simon asked Feb 4 at 12:33 SimonSimon 32 bronze badges 2
  • Please provide enough code so others can better understand or reproduce the problem. – Community Bot Commented Feb 4 at 15:54
  • As the bot says, but also provide logfiles. The code you provided should work. It's not recommended, but it should work. Fernando possibly explains why. But as Fernando, I highly recommend not hosting two endpoints in a single process. It's doable, but not recommended. – Dennis van der Stelt Commented Feb 5 at 13:23
Add a comment  | 

1 Answer 1

Reset to default 0

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:

Project A

// 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);

Project B

// 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);

If you truly need host more than one endpoint

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.

发布评论

评论列表(0)

  1. 暂无评论