title: How to configure constructor injection in NServiceBus
body: <p>I have what would seem to be a relatively common scenario. I need to inject a dependency which  requires a constructor.</p>
  
  <p>I have a repository that looks like this:</p>
  
  <pre><code>public class ApprovalRepository : IApprovalRepository
  {
      private readonly MongoCollection&lt;Approval&gt; _collection;
  
      public ApprovalRepository(MongoDatabase database)
      {
          this._collection = database.GetCollection&lt;Approval&gt;("Approvals");
      }
  
      // ...
  }
  </code></pre>
  
  <p>An endpoint config that looks like this:</p>
  
  <pre><code>public class EndpointConfig : IConfigureThisEndpoint, 
      AsA_Server, IWantCustomInitialization
  {
      public void Init()
      {
          NServiceBus.Configure.With().DefaultBuilder().JsonSerializer();
      }
  }
  </code></pre>
  
  <p>And a handler that looks like this:</p>
  
  <pre><code>public class PlaceApprovalHandler : IHandleMessages&lt;PlaceApproval&gt;
  {
      public IApprovalRepository ApprovalRepository { get; set; }
  
      public void Handle(PlaceApproval message)
      {
          //
          ApprovalRepository.Save(
              new Approval
                  {
                      Id = message.Id, 
                      Message = message.MessageText, 
                      ProcessedOn = DateTime.UtcNow
                  });
      }
  }
  </code></pre>
  
  <p>I then have a class to do the custom initialization:</p>
  
  <pre><code>public class ConfigureDependencies : IWantCustomInitialization
  {
      public void Init()
      {
          // configure Mongo
          var client = new MongoClient("mongodb://localhost:27017/?safe=true");
          var server = client.GetServer();
          var database = server.GetDatabase("ServiceBusTest");
  
          Configure.Instance.Configurer.RegisterSingleton&lt;IApprovalRepository&gt;(new ApprovalRepository(database));
      }
  }
  </code></pre>
  
  <p>The result is an error:</p>
  
  <pre><code>2013-04-11 17:01:03,945 [Worker.13] WARN  NServiceBus.Unicast.UnicastBus [(null)] &lt;(null)&gt; - PlaceApprovalHandler failed handling message.
  Autofac.Core.DependencyResolutionException: Circular component dependency detected: Server.PlaceApprovalHandler -&gt; Server.ApprovalRepository -&gt; Server.ApprovalRepository.
     at Autofac.Core.Resolving.CircularDependencyDetector.CheckForCircularDependency(IComponentRegistration registration, Stack`1 activationStack, Int32 callDepth) in :line 0
     at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) in :line 0
  </code></pre>
  
  <p>I'm not that familiar with Autofac. I also tried the following, which also had a similar result:</p>
  
  <pre><code>Configure.Instance.Configurer.ConfigureComponent(() =&gt; new ApprovalRepository(database), DependencyLifecycle.SingleInstance)
  </code></pre>