title: How to Inject dependencies in a Unity IoC container for SignalR hub?
body: <p>I have an application that is written with c# on the top of the ASP.NET MVC 5 Framework. I implemented <a href="https://www.nuget.org/packages/Unity.Mvc/" rel="nofollow noreferrer">Unity.Mvc</a> into my project. Now, I am trying to inject dependencies objects into my <a href="https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr-and-mvc" rel="nofollow noreferrer">SignalR Hub</a>.</p>
  
  <p>I created a class called <code>UnityHubActivator</code></p>
  
  <p>My class looks like this</p>
  
  <pre><code>public class UnityHubActivator : IHubActivator
  {
      private readonly IUnityContainer _container;
  
      public UnityHubActivator(IUnityContainer container)
      {
          _container = container;
      }
  
      public IHub Create(HubDescriptor descriptor)
      {
          return (IHub)_container.Resolve(descriptor.HubType);
      }
  }
  </code></pre>
  
  <p>Then in my <code>UnityConfig</code> class I added the following to my <code>RegisterTypes</code> method</p>
  
  <pre><code>var unityHubActivator = new UnityHubActivator(container);
  
  container.RegisterInstance&lt;IHubActivator&gt;(unityHubActivator);
  </code></pre>
  
  <p>My hub looks like this</p>
  
  <pre><code>[Authorize]
  public class ChatHub : Hub
  {
      protected IUnitOfWork UnitOfWork { get; set; }
  
      public ChatHub(IUnitOfWork unitOfWork)
          : base()
      {
          UnitOfWork = unitOfWork;
      }
  
  }
  </code></pre>
  
  <p>But when I run the hub, the constructor never get called and the connection never takes place.</p>
  
  <p>How can I correctly use <code>Unity</code> framework to inject dependencies into my hub?</p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>I tried to created a custom container like so</p>
  
  <pre><code>public class UnitySignalRDependencyResolver: DefaultDependencyResolver
  {
      protected IUnityContainer Container;
      private bool IsDisposed = false;
  
      public UnitySignalRDependencyResolver(IUnityContainer container)
      {
          if (container == null)
          {
              throw new ArgumentNullException("container");
          }
  
          Container = container.CreateChildContainer();
      }
  
      public override object GetService(Type serviceType)
      {
          if (Container.IsRegistered(serviceType))
          {
              return Container.Resolve(serviceType);
          }
  
          return base.GetService(serviceType);
      }
  
      public override IEnumerable&lt;object&gt; GetServices(Type serviceType)
      {
          if (Container.IsRegistered(serviceType))
          {
              return Container.ResolveAll(serviceType);
          }
  
          return base.GetServices(serviceType);
      }
  
      protected override void Dispose(bool disposing)
      {
          base.Dispose(disposing);
  
          if(IsDisposed)
          {
              return;
          }
  
          if(disposing)
          {
              Container.Dispose();
          }
  
          IsDisposed = true;
      }
  } 
  </code></pre>
  
  <p>Then here is how I configured the hub in the Startup class</p>
  
  <pre><code>public class Startup
  {
      public IUnityContainer Container { get; set; }
      public Startup(IUnityContainer container)
      {
          Container = container;
      }
  
      public void Configuration(IAppBuilder app)
      {
          app.Map("/signalr", map =&gt;
          {
              var resolver = new UnitySignalRDependencyResolver(Container);
  
              var hubConfiguration = new HubConfiguration
              {
                  Resolver = resolver
              };
  
              map.RunSignalR(hubConfiguration);
          });
      }
  }
  </code></pre>
  
  <p>But still now working... the hub constructor never get called.</p>
  
  <p>Here is how I am calling my hub from the client</p>
  
  <pre><code>&lt;script src="~/Scripts/jquery.signalR-2.2.2.min.js"&gt;&lt;/script&gt;
  &lt;script src="~/signalr/hubs"&gt;&lt;/script&gt;
  &lt;script&gt;
  
  $(function () {
      // Reference the auto-generated proxy for the hub.
      var app = $.connection.chatHub;
      console.log('Getting things ready....');
  
      app.client.outOfTasks = function () {
          console.log('Nothing to do here')
      };
  
      app.client.logError = function (message) {
          console.log(message)
      };
  
      app.client.logNote = function (message) {
          console.log(message)
      };
  
      app.client.assignTask = function (taskId) {
          app.server.taskReceived();
          console.log('task received!!!' + taskId);
  
      };
  
      // Start the connection.
      $.connection.hub.start().done(function () {
          console.log('Connection Started....');
      });
  });
  
  &lt;/script&gt;
  </code></pre>