バックグラウンド ジョブを実行できるように、Hangfire.Ninject パッケージを ASP MVC 5 アプリケーションにインストールしました。
ドキュメントを読みましたが、実装方法について困惑しています。
私の既存の構成では、IUnitOfwork クラスに InRequestScope を使用して、次のように HTTP 要求ごとに 1 つのインスタンスのみがインスタンス化されるようにします。
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
}
ドキュメントに従って hangfire で 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.
hangfire を使用してバックグラウンド ジョブを処理するために使用したいクラスは次のとおりです。
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 クラスが 1 つだけ使用されるようにしたいと考えています。InRequestScope を削除すると、アプリケーションの残りの部分にどのような影響がありますか?