2017-07-17 40 views
2

我有ASP.NET Web API應用程序。該應用程序使用Unity作爲IoC容器。該應用程序也使用了Hangfire,我正在嘗試將Hangfire配置爲使用Unity。如何用統一配置hangfire?

因此,根據documentation我使用Hangfire.Unity它將Unity容器註冊爲Hangfire中的當前作業激活器。

我有這甚至配置Hangfire.Unity後對IBackgroundJobClient

public class MyService 
{ 
    private MyDBContext _dbContext = null; 
    private IBackgroundJobClient _backgroundJobClient = null; 

    public MyService(MyDbContext dbContext, IBackgroundJobClient backgroundJobClient) 
    { 
    _dbContext = dbContext; 
    _backgroundJobClient = backgroundJobClient; 
    } 
} 

但是依賴一個類就不能創建的BackgroundJobClient

&通實例,所以我不得不註冊團結容器BackgroundJobClient每個依賴。

統一登記

​​

OWIN啓動

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<MyService>(x => x.Prepare(), Cron.MinuteInterval(10)); 
     } 
    } 

代碼正在與此類配置。不過,我有問題:

這是使用Hangfire配置Unity的正確方法嗎?

爲什麼我需要在OWIN啓動時調用Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireConnectionString"),即使SqlServerStorage已經在Unity容器中註冊爲JobStorage

如果我不在OWIN啓動時調用UseSqlServerStorage()方法,那麼我在app.UseHangfireDashboard()方法上得到異常。

JobStorage.Current屬性值尚未初始化。在使用Hangfire客戶端或服務器API之前,您必須先設置 。

回答

0

我認爲存在一個問題,您想要在Unity生態系統外啓動Hangfire,但也希望Unity瞭解如何實例化相關實施的相應Hangfire接口。由於Hangfire本身不使用Unity,因此需要使用適當的配置(例如SQL Server連接字符串)啓動Hangfire,然後使用該配置通知Unity如何實例化Hangfire界面。通過設置SQL的全局Hangfire配置,然後使用相同的Hangfire靜態實例設置Unity,我能夠解決此問題。

這裏的示例代碼,其中第一,你會看到我是如何開始的遲髮型儀表板和服務器連接字符串:

public void Configuration(IAppBuilder app) 
{ 
    var configuration = new Configuration(); // whatever this is for you 

    GlobalConfiguration.Configuration.UseSqlServerStorage(
     configuration.GetConnectionString()); 

    GlobalConfiguration.Configuration.UseActivator(
     new HangfireContainerActivator(UnityConfig.GetConfiguredContainer())); 

    app.UseHangfireDashboard("/hangfire", new DashboardOptions 
    { 
     Authorization = new[] {new HangfireAuthorizationFilter()} 
    }); 
    app.UseHangfireServer(); 
} 

作爲第二個例子,這裏的團結爲遲髮型的配置;請注意此代碼如何使用靜態的JobStorage Hangfire對象來實例化JobStorage的任何請求。

public static void RegisterHangfire(IUnityContainer container) 
    { 
     container.RegisterType<JobStorage>(new InjectionFactory(c => JobStorage.Current)); 
     container.RegisterType<IJobFilterProvider, JobFilterAttributeFilterProvider>(new InjectionConstructor(true)); 
     container.RegisterType<IBackgroundJobFactory, BackgroundJobFactory>(); 
     container.RegisterType<IRecurringJobManager, RecurringJobManager>(); 
     container.RegisterType<IBackgroundJobClient, BackgroundJobClient>(); 
     container.RegisterType<IBackgroundJobStateChanger, BackgroundJobStateChanger>(); 
    } 

我相信這種方法爲您提供了兩全其美,你只設置你的SQL Server連接一旦你做到這一點早開球遲髮型,但你使用實例來告訴團結如何做人。