title: Exception while creating NServiceBus with CustomConfigurationSource
body: <p>I have a need to create an NServiceBus using CustomConfigurationSource to replace my app.config.</p>
  
  <p>I have implemented CustomConfiguration source in my code, but I get the following exception when creating the bus:</p>
  
  <p>System.NullReferenceException was unhandled
    HResult=-2147467261
    Message=Object reference not set to an instance of an object.
    Source=NServiceBus.Core
    StackTrace:
         at NServiceBus.Unicast.UnicastBus.NServiceBus.IStartableBus.Start(Action startupAction) in c:\TeamCity\buildAgent\work\nsb.master_10\src\unicast\NServiceBus.Unicast\UnicastBus.cs:line 852</p>
  
  <p>I create my bus in class A as follows:</p>
  
  <pre><code>public class A: IA
  {
      public static IBus Bus { get; private set; }
  
      public A()
      {
          CreateBus();
      }
  
      private void CreateBus()
      {
          Bus = NServiceBus.Configure.With()
              .CustomConfigurationSource(new AConfigSource())
              .Log4Net()
              .DefaultBuilder()
              .XmlSerializer()
              .MsmqTransport()
                  .IsTransactional(false)
                  .PurgeOnStartup(false)
              .UnicastBus()
                  .LoadMessageHandlers()
                  .ImpersonateSender(false)
              .CreateBus()
              .Start(() =&gt; Configure.Instance.ForInstallationOn&lt;NServiceBus.Installation.Environments.Windows&gt;().Install());
              // Exception thrown here ******
      }
  }
  </code></pre>
  
  <p>My configuration class:</p>
  
  <pre><code>public class AConfigSource: IConfigurationSource
  {
      #region Implementation of IConfigurationSource
  
      public T GetConfiguration&lt;T&gt;() where T : class, new()
      {
          if (typeof(T) == typeof(MessageForwardingInCaseOfFaultConfig))
              return MessageForwardingInCaseOfFaultConfiguration() as T;
  
          if (typeof(T) == typeof(MsmqTransportConfig))
              return MsmqTransportConfiguration() as T;
  
          if (typeof(T) == typeof(UnicastBusConfig))
              return UnicastBusConfiguration() as T;
  
          // leaving the rest of the configuration as is:
          return ConfigurationManager.GetSection(typeof(T).Name) as T;
  
      }
  
      #endregion Implementation of IConfigurationSource
  
      #region Private
  
      private MessageForwardingInCaseOfFaultConfig MessageForwardingInCaseOfFaultConfiguration()
      {
          return new MessageForwardingInCaseOfFaultConfig
          {
              ErrorQueue = "error"
          };
      }
  
      private UnicastBusConfig UnicastBusConfiguration()
      {
          return new UnicastBusConfig();
      }
  
      private MsmqTransportConfig MsmqTransportConfiguration()
      {
          return new MsmqTransportConfig
          {
              ErrorQueue = "error",
              InputQueue = "myInputQueue",
              MaxRetries = 5,
              NumberOfWorkerThreads = 1
          };
      }
  
      #endregion Private
  }
  </code></pre>
  
  <p>What causes the NullReferenceException?  What steps have I missed to override all my nservicebus configurations?</p>
  
  <p>Update:
  I am using NServiceBus version 3.3.8.</p>
  
  <p>Update:
  Based upon John's answer, I changed my private UnicastBusConfiguration method as follows:</p>
  
  <pre><code>    private UnicastBusConfig UnicastBusConfiguration()
      {
          return new UnicastBusConfig
          {
              ForwardReceivedMessagesTo = "",
              MessageEndpointMappings = new MessageEndpointMappingCollection()
          };
      }
  </code></pre>
  
  <p>I still get the same exception mentioned above.</p>
  
  <p>Here is the config I need to run in class A:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;configSections&gt;
      &lt;section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core" /&gt;
      &lt;section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" /&gt;
      &lt;section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" /&gt;
    &lt;/configSections&gt;
    &lt;MsmqTransportConfig ErrorQueue="error" NumberOfWorkerThreads="1" MaxRetries="5" /&gt;
    &lt;UnicastBusConfig ForwardReceivedMessagesTo=""&gt;
      &lt;MessageEndpointMappings&gt;&lt;/MessageEndpointMappings&gt;
    &lt;/UnicastBusConfig&gt;
    &lt;MessageForwardingInCaseOfFaultConfig ErrorQueue="error" /&gt;
    &lt;runtime&gt;
      &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="NServiceBus" publicKeyToken="9fc386479f8a226c" culture="neutral" /&gt;
          &lt;bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="NServiceBus.Core" publicKeyToken="9fc386479f8a226c" culture="neutral" /&gt;
          &lt;bindingRedirect oldVersion="0.0.0.0-3.3.0.0" newVersion="3.3.0.0" /&gt;
        &lt;/dependentAssembly&gt;
      &lt;/assemblyBinding&gt;
    &lt;/runtime&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>More information: I am running a WPF application. I added a class library to my solution to host nservicebus. I create an instance of object "Class A" mentioned above.  I need nservicebus to configure based upon app.config for class A, not app.config for my WPF startup project.</p>
  
  <p>Maybe CustomConfigurationSource is overkill?  Is there a better way to command NServiceBus to configure using app.config in Class A project?</p>
  
  <p>Update: I receive the following error that indicates a missing UnicastBus.cs class.  I have reference to NserviceBus.Host in my project...</p>
  
  <p>Locating source for 'c:\TeamCity\buildAgent\work\nsb.master_10\src\unicast\NServiceBus.Unicast\UnicastBus.cs'. (No checksum.)
  The file 'c:\TeamCity\buildAgent\work\nsb.master_10\src\unicast\NServiceBus.Unicast\UnicastBus.cs' does not exist.
  Looking in script documents for 'c:\TeamCity\buildAgent\work\nsb.master_10\src\unicast\NServiceBus.Unicast\UnicastBus.cs'...
  Looking in the projects for 'c:\TeamCity\buildAgent\work\nsb.master_10\src\unicast\NServiceBus.Unicast\UnicastBus.cs'.
  The file was not found in a project.</p>