Ninject移除/替换InThreadScope对象实例



我有一个实体框架上下文被注入到后台作业中(用Hangfire.io处理)。Hangfire为每个后台工作者生成线程。。。但是在同一个工人身上运行多个作业。因此,我的EF上下文可能会保留很长一段时间。

我想在作业线程执行结束时删除它的Ninject实例。从而使它在同一线程中为该类型的下一个解析创建一个新实例。

如何在Ninject中从InThreadScope中删除实例?

Hangfire可以通过实现IServerFilter的JobFilterAttribute通知您作业执行(之前)和执行(之后),基本上类似于:

public class MyJobAttribute : JobFilterAttribute, IServerFilter
{
    public void OnPerformed(PerformedContext performedContext)
    {
        //here you'll be called on the same thread of the job after it has been executed
    }
    public void OnPerforming(PerformingContext performingContext)
    {
        //here you'll be called on the same thread of the job that will be executed
    }
}

只需将MyJobAttribute应用于工作的类/方法

最新更新