Subscription fails when configured after bus

47 views
Skip to first unread message

Robert Witt

unread,
Oct 27, 2017, 3:08:32 PM10/27/17
to masstransit-discuss
Hi, I'd like to be able to configure endpoints on a host, AFTER I've configured the bus, but BEFORE I've started it. Perhaps I'm mistaken, but I think that should be possible. 

However, I'm running into a problem. This code works fine (configuring the bus, the host, and attaching the endpoints, all inside the bus configurator), and the bus starts normally.

IServiceBusHost azBusHost = null;
        var bus = MassTransit.Bus.Factory.CreateUsingAzureServiceBus(cfg =>
{
azBusHost = cfg.Host(new Uri("sb://somehost.servicebus.windows.net"), host =>
{
// azure config info
});

cfg.SubscriptionEndpoint<BasicCommand>(azBusHost, "basiccommand-" + Environment.MachineName, e =>
{
e.Consumer<BasicCommandConsumer>();
e.DefaultMessageTimeToLive = TimeSpan.FromDays(1);
e.EnableDeadLetteringOnMessageExpiration = false;
});

cfg.SubscriptionEndpoint<BasicMessage>(azBusHost, "basicmessage-" + Environment.MachineName, e =>
{
e.Consumer<BasicMessageConsumer>();
e.DefaultMessageTimeToLive = TimeSpan.FromDays(1);
e.EnableDeadLetteringOnMessageExpiration = false;
});
});

But, when I try to configure subscriptions after the bus is configured, then when I start the bus, I get an exception saying "An item with the same key has already been added." The item in question is a subscription endpoint, and the exception is thrown from ReceiveEndpointCollection.StartEndpoint. Debugging through the class, I can see that "StartEndpoints" is called once, with a collection of three endpoints (all of different names). Subsequently, "StartEndpoint" is called three times, with those three endpoints. However, StartEndpoint is then called a fourth time, with one of my subscription endpoints. That's what causes the duplicate key error.

Any thoughts on why StartEndpoint might be called more than three times, and why it might differ based on when the subscription endpoints are defined?

For reference:
Here's the code that fails with the exception:

        IServiceBusHost azBusHost = null;
var bus = MassTransit.Bus.Factory.CreateUsingAzureServiceBus(cfg =>
{
azBusHost = cfg.Host(new Uri("sb://somehost.servicebus.windows.net"), host =>
{
// azure config info
});
});
azBusHost.ConnectSubscriptionEndpoint<BasicCommand>("basicmessage-" + Environment.MachineName, e =>
{
e.Consumer<BasicCommandConsumer>();
e.DefaultMessageTimeToLive = TimeSpan.FromDays(1);
e.EnableDeadLetteringOnMessageExpiration = false;
});

azBusHost.ConnectSubscriptionEndpoint<BasicMessage>("basiccommand-" + Environment.MachineName, e =>
{
e.Consumer<BasicMessageConsumer>();
e.DefaultMessageTimeToLive = TimeSpan.FromDays(1);
e.EnableDeadLetteringOnMessageExpiration = false;
});

Thanks in advance for any insight you can provide.
Rob Witt

Chris Patterson

unread,
Oct 27, 2017, 3:53:52 PM10/27/17
to masstrans...@googlegroups.com
ConnectSubscriptionEndpoint (and ConnectReceiveEndpoint) should be called after Start has been called.


--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstransit-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/33d04626-f078-4b63-9088-fc5d487fd70a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Witt

unread,
Oct 28, 2017, 11:03:57 AM10/28/17
to masstransit-discuss
Super, thank you! I remember reading that the bus is immutable once started; I'm not sure why I thought that meant it couldn't connect new subscriptions. Thanks again.


Reply all
Reply to author
Forward
0 new messages