0

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

我想在执行结束时删除 Job 线程的 Ninject 实例。从而导致它在同一线程中为该类型的下一个分辨率创建一个新实例。

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

4

1 回答 1

0

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您工作的课程/方法

于 2015-02-24T14:14:05.910 回答