Structuremap interferes with nServicebus EndpointName configuration

When creating a nServiceBus host with an EndpointName in the configuration be warned for this structuremap interference. See the repro code below of a bug we recently fixed. The messages and handlers are left out of the sample for simplicity.

Bug

The IoC container is initialized by scanning all types in the current assembly.

public class IoC {
    public static void Init() {
        ObjectFactory.Initialize(factory => {
            factory.Scan(scan => {
                scan.TheCallingAssembly();
            });
        });
    }
}

Initialization of the nServicebus with IoC and a name for the endpoint (queue)

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, 
                              IWantCustomInitialization {
    public void Init() {
        IoC.Init();
        // expect name of queue to be CityBus
        Configure.With()
                 .StructureMapBuilder(ObjectFactory.Container)
                 .DefineEndpointName("CityBus");
    }
}

But when the project starts the output window tells me the queue is created with the default naming convention using the assembly namespace. The retries, timeouts and timeoutsdispatcher queue are correct. The main queue is (wrongly) named StructureMap.QueueName after the namespace.
nservicebus.queue.namespace.convention

Solution

The structuremap scan will create the endpoint with default convention as it executed before the DefineEndpointName. By moving the code before the scan the correct queue is created.

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, 
                              IWantCustomInitialization {
    public void Init() {
        // expect name of queue to be CityBus
        Configure.With().DefineEndpointName("CityBus");
        IoC.Init();
        Configure.With()
                 .StructureMapBuilder(ObjectFactory.Container);
    }
}

nservicebus.queue.endpointname

Now manually remove the StructureMap.QueueName as it is no longer needed.

Full repro project download here.

About erictummers

My work as a recruited developer changes almost every month. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s