【问题标题】:Hangfire with Ninject用 Ninject 挂火
【发布时间】:2015-06-01 22:11:26
【问题描述】:

我正在尝试将 Hangfire 与 Ninject 一起使用。

这是我的问题,我的项目布局如下:

.Sln
    |- Core
    |- Web

现在Core中是Hangfire方法:

public class Scheduler
{
    public void HangfireIoc()
    {
        BackgroundJob.Enqueue<MovieSaver>(x => x.SaveMovies());
    }
}

MovieSaver 类在 (Core):

public class MovieSaver
{
    public IMovieApi Api { get; set; }

    public MovieSaver(IMovieApi api)
    {
        Api = api;
    }
   //Other methods
}

Startup.cs(在Web

public void Configuration(IAppBuilder app)
{
  var kernal = new StandardKernel();
  GlobalConfiguration.Configuration.UseNinjectActivator(kernal);

  app.UseHangfireDashboard();
  app.UseHangfireServer();
}

我在Core 中有一个用于 Ninject 的 Bindings 类:

public class Bindings : NinjectModule
{
    public override void Load()
    {
        Bind<IMovieApi>().To<MovieApi>();
    }
} 

但似乎当 Hangfire 开始我得到的工作时:

Ninject.ActivationException

Error activating IMovieApi No matching bindings are available, and the type is not self-bindable.
 Activation path: 2) Injection of dependency IMovieApi into parameter api of constructor of type MovieSaver 
1) Request for MovieSaver Suggestions: 1) Ensure that you have defined a binding for IMovieApi. 
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 
3) Ensure you have not accidentally created more than one kernel. 
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 
5) If you are using automatic module loading, ensure the search path and filters are correct.

我是 Ninject 和 IoC 容器的新手。

谁能帮忙?

【问题讨论】:

    标签: c# ninject ioc-container hangfire


    【解决方案1】:

    Bindings 模块未加载。尝试添加

    kernel.Load<Bindings>();
    

    到 Startup.cs 的 Configuration 方法。

    您还可以通过调用将所有Modules 加载到程序集中:

    kernel.Load(someAssembly);
    

    例如:

    kernel.Load(typeof(Startup).Assembly);
    

    【讨论】:

      【解决方案2】:

      新建 Ninject.Web.Common.Bootstrapper().Kernel:

      public void Configuration(IAppBuilder app)
          {            
      
              ConfigureAuth(app);
      
              GlobalConfiguration.Configuration
                  .UseSqlServerStorage("ConnectString");
      
              GlobalConfiguration.Configuration.UseNinjectActivator(new Ninject.Web.Common.Bootstrapper().Kernel);
      
      
              app.UseHangfireDashboard();
              app.UseHangfireServer();
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-29
        相关资源
        最近更新 更多