title: SignalR Dependency Injection for ASP.NET Core v2.0 using Autofac
body: <p>Is it possible to use dependency injection to inject dependencies into SignalR on ASP.NET Core v2.0?</p>
  
  <p>Assuming the following hub and dependency:</p>
  
  <pre><code>public MyHub : Hub {
  
      private readonly IMyDependency dependency;
  
      public MyHub(IMyDependency dependency) {
          this.dependency = dependency;
      }
  }
  
  public void MyDependency : IDependency
  {
      public void MyMethod() {
          Console.WriteLine("I'm a dependency!");
      }
  }
  </code></pre>
  
  <p>I've scoured a bit of the web and there isn't anything obvious out there.  I <a href="https://geeks.ms/clanderas/2016/11/03/asp-net-core-with-signalr-server-and-autofac/" rel="noreferrer">found this tutorial</a> which at first seemed quite promising until I realised it was for <code>Microsoft.AspNetCore.SignalR.Server</code> which didn't ship in the end.</p>
  
  <p>At the moment I have the following setup using Autofac and it's not working:</p>
  
  <pre><code>public class Startup
  {
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
  
      public IConfiguration Configuration { get; }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public IServiceProvider ConfigureServices(IServiceCollection services)
      {
          services.AddMvc();
          services.AddSignalR();
  
          // Configue Autofac
          var containerBuilder = new ContainerBuilder();
  
          containerBuilder.RegisterModule&lt;MyModule&gt;();
  
          // Configure SignalR hubs for dependency injection
  containerBuilder.RegisterSignalRHubs(typeof(Startup).GetTypeInfo().Assembly);
  
          containerBuilder.Populate(services);
          var container = containerBuilder.Build();
          return new AutofacServiceProvider(container);
      }
  }
  
  public static class AutoFacExtensions
  {
      public static IRegistrationBuilder&lt;object, ScanningActivatorData, DynamicRegistrationStyle&gt; RegisterSignalRHubs(this ContainerBuilder builder, params Assembly[] assemblies)
      {
          return builder.RegisterAssemblyTypes(assemblies)
              .Where(t =&gt; typeof(IHub).IsAssignableFrom(t))
              .ExternallyOwned();
      }
  }
  
  public class MyModule : Module
  {
      protected override void Load(ContainerBuilder builder)
      {
          builder.RegisterType&lt;MyDependency&gt;().As&lt;IMyDependency&gt;();
      }
  }
  </code></pre>
  
  <p>It looks like the <code>IHub</code> interface doesn't exist anymore.  I tried <code>IHubContext&lt;MyHub&gt;</code> in the hope that this might work with the latest version but sadly not.</p>
  
  <p>When I have dependencies in my hub's constructor, the hub isn't created <s>despite all of the dependencies registered with Autofac</s>.</p>
  
  <p>How can I achieve this with the lastest version <code>1.0.0.0-alpha2-final</code>?</p>