3
我已將Hangfire.Ninject程序包安裝到ASP MVC 5應用程序,以便我可以運行一些後臺作業。使用InRequestScope與Ninject進行Hangfire
我已經閱讀了文檔,但我對如何實現它感到困惑。
我的現有配置使用InRequestScope我IUnitOfwork類,以確保只有一個實例,每個HTTP請求如下實例:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
}
要ninject與具有遲髮型跟着我如下更新了配置文件使用在我ninjectwebcommon.cs類:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
GlobalConfiguration.Configuration.UseNinjectActivator(kernel);
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUnitOfWork>()
.ToSelf()
.InNamedOrBackgroundJobScope(context => context.Kernel.Components.GetAll<INinjectHttpApplicationPlugin>()
.Select(c => c.GetRequestScope(context))
.FirstOrDefault(s => s != null));
}
但現在我得到以下錯誤:
Error activating IUnitOfWork using self-binding of IUnitOfWork
No constructor was available to create an instance of the implementation type.
我有一個類我想用用遲髮型來處理我的後臺作業如下:
public class EmailJob
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMailer _mailer;
public EmailJob(IUnitOfWork unitOfWork, IMailer mailer)
{
_unitOfWork = unitOfWork;
_notificationMailer = notificationMailer;
}
public void Execute()
{
// DO Stuff
}
}
任何一個知道我在做什麼錯?該文件還指出:
Services registered with InRequestScope() directive will be unavailable during job activation, you should re-register these services without this hint.
這是什麼意思?我仍然希望確保每個http請求只使用一個實現dbContext的IUnitOfwork類。如果我刪除InRequestScope,現在如何影響應用程序的其餘部分?