Search code examples
nservicebusstructuremapobjectfactory

NServiceBus: Unable to set the value for key: NServiceBus.Transport.ConnectionString


My application I'm working on uses NServiceBus for a messaging bus for some operations. In my AppStartup, I'm trying to configure NServiceBus to use a custom Unicast config, which is running successfully it appears. However, when I call my Configure with this code:

    ObjectFactory.Configure(configure =>
        configure.For<IBus>().Use(
            Configure
                .With()
                .StructureMapBuilder(ObjectFactory.Container)
                .UseTransport<Msmq>()
                .UnicastBus()
                .SendOnly()
            )
        );

I get this exception:

Unable to set the value for key: NServiceBus.Transport.ConnectionString. The settings has been locked for modifications. Please move any configuration code earlier in the configuration pipeline

Here's the stack trace for the exception (confidential parts omitted):

   at NServiceBus.Settings.SettingsHolder.EnsureWriteEnabled(String key) in c:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Settings\SettingsHolder.cs:line 174
   at NServiceBus.Settings.SettingsHolder.Set(String key, Object value) in c:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Settings\SettingsHolder.cs:line 57
   at NServiceBus.Transports.ConfigureTransport`1.Configure(Configure config) in c:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Transports\ConfigureTransport.cs:line 21
   at NServiceBus.TransportReceiverConfig.UseTransport(Configure config, Type transportDefinitionType, String connectionStringName) in c:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\Transport\Config\TransportReceiverConfig.cs:line 55
   at NServiceBus.TransportReceiverConfig.UseTransport[T](Configure config, String connectionStringName) in c:\BuildAgent\work\31f8c64a6e8a2d7c\src\NServiceBus.Core\Unicast\Transport\Config\TransportReceiverConfig.cs:line 22
   at ...
   at StructureMap.Container.Configure(Action`1 configure) in c:\BuildAgent\work\767273992e840853\src\StructureMap\Container.cs:line 325
   at StructureMap.ObjectFactory.Configure(Action`1 configure) in c:\BuildAgent\work\767273992e840853\src\StructureMap\ObjectFactory.cs:line 386

To be clear: this is on the client side, not on the actual bus side of the NServiceBus. This setup is taking place to be able to send messages to the NServiceBus queue, which is working properly already with another application. I've also confirmed that this application is configured the same as the other application.

Any help I could get on this would be great :)


Solution

  • RESOLVED!

    For reference: the Bus object was being instantiated in the IOC configuration, but then the IOC was trying to repeat the process. This led to multiple instances of the IBus object being instantiated, which confused IOC since an IOC object should only be instantiated once according to StructureMap. I resolved this by surrounding my code with a check to see if the IBus object had been instantiated by my IOC controller or not before running configuration functions.