title: Problems integrating nServiceBus with StructureMap
body: <p>I'm trying to use StructureMap with nServiceBus. </p>
  
  <h2>The Project:</h2>
  
  <ul>
  <li>Uses a GenericHost Endpoint to send command messages</li>
  <li>Configures nServiceBus using the StructMapBuilder.</li>
  <li>Uses a simple StructureMap registry config</li>
  <li>Uses a start up class TestServer supporting IWantToRunAtStartup</li>
  <li>The TestServer class has ctor dependency on a TestManager class</li>
  <li>The TestManager class has ctor dependency on IBus </li>
  </ul>
  
  <p>ObjectFactory.WhatDoIHave() shows StructureMap knows how to construct the classes.</p>
  
  <p>When run I get buildup errors. nServiceBus seems to be overwriting the config? </p>
  
  <p>Note that when I add a IBus ctor depenendency to my event handlers without any other config all appears fine.</p>
  
  <h2>Error:</h2>
  
  <blockquote>
    <p>Exception when starting endpoint, error has been logged. Reason: Error creating object with name 'nSeviceBusStructureMapTest.TestServer' : Unsatisfied dependency expressed through constructor argument with index 0 of type [nSeviceBusStructureMapTest.ITestManager] : No unique object of type [nSeviceBusStructureMapTest.ITestManager] is defined : Unsatisfied dependency of type [nSeviceBusStructureMapTest.ITestManager]: expected at least 1 matching object to wire the [miningServiceManage] parameter on the constructor of object [nSeviceBusStructureMapTest.TestServer]</p>
  </blockquote>
  
  <h2>Source:</h2>
  
  <pre><code>using System;
  using System.Diagnostics;
  using NServiceBus;
  using StructureMap;
  using StructureMap.Configuration.DSL;
  
  namespace nSeviceBusStructureMapTest
  {
      public class TestSmRegistry : Registry
      {
          public TestSmRegistry()
          {
              For&lt;ITestManager&gt;().Use&lt;TestManager&gt;();
              For&lt;TestServer&gt;().Use&lt;TestServer&gt;();
          }
      }
  
      public class TestEndPoint : AsA_Server, IConfigureThisEndpoint
      {
          public void Init()
          {
              Configure.With().StructureMapBuilder(ObjectFactory.Container);
              ObjectFactory.Configure(c =&gt; c.AddRegistry&lt;TestSmRegistry&gt;());
              Debug.WriteLine(ObjectFactory.WhatDoIHave());
          }
      }
  
      public class TestServer : IWantToRunAtStartup
      {
          public TestServer(ITestManager miningServiceManage)
          {
              _miningServiceManage = miningServiceManage;
          }
          private readonly ITestManager _miningServiceManage;
          public void Run()
          {
              _miningServiceManage.Run();
          }
          public void Stop() { }
      }
  
      public interface ITestManager
      {
          void Run();
      }
  
      public class TestManager : ITestManager
      {
          public TestManager(IBus bus)
          {
              _bus = bus;
          }
          private readonly IBus _bus;
  
          public void Run()
          {
              if (_bus == null) Debug.WriteLine("Error no bus");
              // Send messages on bus;
          }
      }
  }
  </code></pre>
  
  <p>
      
        
          
          
        </p>
  
  <pre><code>  &lt;MsmqTransportConfig InputQueue="test" ErrorQueue="error" NumberOfWorkerThreads="1" MaxRetries="5"  /&gt;
  
    &lt;UnicastBusConfig&gt;
      &lt;MessageEndpointMappings&gt;
      &lt;/MessageEndpointMappings&gt;
    &lt;/UnicastBusConfig&gt;
  </code></pre>
  
  <p></p>
  
  <p>Any ideas?</p>