标签: hangfire-unity

如何使用unity配置hangfire?

我有 ASP.NET Web API 应用程序。该应用程序使用 Unity 作为 IoC 容器。该应用程序也在使用 Hangfire,我正在尝试配置 Hangfire 以使用 Unity。

因此,根据文档,我使用Hangfire.Unity它将 Unity 容器注册为 Hangfire 中的当前作业激活器。

我有一个依赖于IBackgroundJobClient

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

   public MyService(MyDbContext dbContext, IBackgroundJobClient backgroundJobClient)
   {
     _dbContext = dbContext;
     _backgroundJobClient = backgroundJobClient;
   }
}
Run Code Online (Sandbox Code Playgroud)

但是,即使在配置之后,Hangfire.Unity它也无法创建并传递实例BackgroundJobClient

所以我必须BackgroundJobClient用统一容器注册每个依赖项。

统一注册

public class UnityConfig
{

    private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();
        RegisterTypes(container);
        return …
Run Code Online (Sandbox Code Playgroud)

hangfire hangfire-unity

5
推荐指数
1
解决办法
2841
查看次数

标签 统计

hangfire ×1

hangfire-unity ×1