title: How to configure hangfire with unity?
body: <p>I have ASP.NET Web API application. The application is using Unity as IoC container. The application is also using Hangfire and I am trying to configure Hangfire to use Unity.   </p>
  
  <p>So based on <a href="http://docs.hangfire.io/en/latest/background-methods/using-ioc-containers.html" rel="noreferrer">documentation</a> i am using <a href="https://github.com/phenixdotnet/Hangfire.Unity" rel="noreferrer">Hangfire.Unity</a> which registers the unity container as a current job activator in Hangfire.</p>
  
  <p>I have a class which has dependency on <code>IBackgroundJobClient</code></p>
  
  <pre><code> public class MyService
   {
     private MyDBContext _dbContext = null;
     private IBackgroundJobClient _backgroundJobClient = null;
  
     public MyService(MyDbContext dbContext, IBackgroundJobClient backgroundJobClient)
     {
       _dbContext = dbContext;
       _backgroundJobClient = backgroundJobClient;
     }
  }
  </code></pre>
  
  <p>However even after configuring <code>Hangfire.Unity</code> it could not create &amp; pass instance of <code>BackgroundJobClient</code></p>
  
  <p>So i had to register every dependency of <code>BackgroundJobClient</code> with unity container.</p>
  
  <p><strong>Unity Registration</strong></p>
  
  <pre><code>public class UnityConfig
  {
  
      private static Lazy&lt;IUnityContainer&gt; container = new Lazy&lt;IUnityContainer&gt;(() =&gt;
      {
          var container = new UnityContainer();
          RegisterTypes(container);
          return container;
      });
  
  
      public static IUnityContainer GetConfiguredContainer()
      {
          return container.Value;
      }
  
      public static void RegisterTypes(IUnityContainer container)
      {
          container.RegisterType&lt;MyDbContext&gt;(new HierarchicalLifetimeManager(), new InjectionFactory(x =&gt; new MyDbContext()));       
  
          // register hangfire dependencies
          container.RegisterType&lt;IBackgroundJobClient, BackgroundJobClient&gt;();
          container.RegisterType&lt;JobStorage, SqlServerStorage&gt;(new InjectionConstructor("HangfireConnectionString"));
          container.RegisterType&lt;IJobFilterProvider, JobFilterAttributeFilterProvider&gt;(new InjectionConstructor(true));
          container.RegisterType&lt;IBackgroundJobFactory, BackgroundJobFactory&gt;();
          container.RegisterType&lt;IRecurringJobManager, RecurringJobManager&gt;();
          container.RegisterType&lt;IBackgroundJobStateChanger, BackgroundJobStateChanger&gt;();
      }
  
  }
  </code></pre>
  
  <p><strong>OWIN Startup</strong></p>
  
  <pre><code>    public class Startup
      {        
          public void Configuration(IAppBuilder app)
          {
              var container = UnityConfig.GetConfiguredContainer();
  
  
              Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireConnectionString");
              Hangfire.GlobalConfiguration.Configuration.UseUnityActivator(container);
  
              // if i dont call UseSqlServerStorage() above then UseHangfireDashboard() method fails with exception
              //JobStorage.Current property value has not been initialized. You must set it before using Hangfire Client or Server API.
  
              app.UseHangfireDashboard();
              app.UseHangfireServer();                      
  
              RecurringJob.AddOrUpdate&lt;MyService&gt;(x =&gt; x.Prepare(), Cron.MinuteInterval(10));
          }
      }
  </code></pre>
  
  <p>Code is working with such configuration. However i have questions:  </p>
  
  <p>Is this the correct way of configuring Unity with Hangfire?   </p>
  
  <p>Why do i need to invoke <code>Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireConnectionString")</code> in OWIN startup even though <code>SqlServerStorage</code> is already registered with Unity container as <code>JobStorage</code>?</p>
  
  <p>If i dont invoke UseSqlServerStorage() method in OWIN startup then i get exception on <code>app.UseHangfireDashboard()</code> method.</p>
  
  <blockquote>
    <p>JobStorage.Current property value has not been initialized. You must
    set it before using Hangfire Client or Server API.</p>
  </blockquote>