title: Resolve different implementations of interface using Autofac
body: <p>I am trying to resolve a different implementation of an interface for different classes using Autofac, the reason being that I want to send updates to clients via a SignalR hub in my message handler - however I don't want the handler to be a hub client itself and different handlers need to use different hubs.</p>
  
  <p>What I had initially looks like this:</p>
  
  <pre><code>public class Hub1 : Hub { }
  public class Hub2 : Hub { }
  
  public class Handler1 : IHandle&lt;Message1&gt;
  {
      private AppData data;
      private IHubContext hubContext;
  
      public Handler1(AppData data)
      {
          this.data = data;
          this.hubContext = GlobalHost.ConnectionManager.GetHubContext&lt;Hub1&gt;()
      }
  }
  
  public class Handler2 : IHandle&lt;Message2&gt;
  {
      private AppData data;
      private IHubContext hubContext;
  
      public Handler2(AppData data)
      {
          this.data = data;
          this.hubContext = GlobalHost.ConnectionManager.GetHubContext&lt;Hub2&gt;()
      }
  }
  </code></pre>
  
  <p>Which works with Autofac bindings as follows:</p>
  
  <pre><code>// Single shared instance of AppData for all handlers
  builder.RegisterType&lt;AppData&gt;().AsSelf().SingleInstance();
  
  builder.RegisterType&lt;Handler1&gt;().AsImplementedInterfaces();
  builder.RegisterType&lt;Handler2&gt;().AsImplementedInterfaces();
  </code></pre>
  
  <p>Both handlers already have <code>AppData</code> injected via Autofac, however I would also like to inject the <code>IHubContext</code> but I am struggling to figure out how to do it.</p>
  
  <p>I have changed to require the <code>IHubContext</code> as a constructor argument in the handlers:</p>
  
  <pre><code>public class Handler1 : IHandle&lt;Message1&gt;
  {
      private AppData data;
      private IHubContext hubContext;
  
      public Handler1(AppData data, IHubContext hubContext)
      {
          this.data = data;
          this.hubContext = hubContext;
      }
  }
  </code></pre>
  
  <p>I have tried registering the <code>IHubContext</code> as a named service and specifying the registration for the handlers as follows:</p>
  
  <pre><code>builder.Register&lt;IHubContext&gt;(_ =&gt; GlobalHost.ConnectionManager.GetHubContext&lt;Hub1&gt;())
      .Named&lt;IHubContext&gt;("Hub1Context");
  
  builder.Register&lt;IHubContext&gt;(_ =&gt; GlobalHost.ConnectionManager.GetHubContext&lt;Hub2&gt;())
      .Named&lt;IHubContext&gt;("Hub2Context");
  
  builder.RegisterType&lt;Handler1&gt;()
      .WithParameter(ResolvedParameter.ForNamed&lt;IHubContext&gt;("Hub1Context"));
  
  builder.RegisterType&lt;Handler2&gt;()
      .WithParameter(ResolvedParameter.ForNamed&lt;IHubContext&gt;("Hub2Context"));
  </code></pre>
  
  <p>However this results in an exception:</p>
  
  <pre><code>A first chance exception of type 'Autofac.Core.DependencyResolutionException' occurred in Autofac.dll
  
  Additional information: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Handler1' can be invoked with the available services and parameters:
  
  Cannot resolve parameter 'Microsoft.AspNet.SignalR.IHubContext hubContext' of constructor 'Void .ctor(AppData, Microsoft.AspNet.SignalR.IHubContext)'.
  </code></pre>
  
  <p>Where am I going wrong? I've done something similar in the past with Ninject by just saying something along the lines of:</p>
  
  <pre><code>kernel.Bind&lt;IHubContext&gt;()
        .ToMethod(_ =&gt; GlobalHost.ConnectionManager.GetHubContext&lt;Hub1&gt;())
        .WhenInjectedInto&lt;Handler1&gt;();
  </code></pre>