2015-02-23 70 views
0

我有一個實體框架上下文被注入後臺作業(使用Hangfire.io處理)。 Hangfire爲每個後臺工作者產生線程......但是在同一個worker上運行多個工作。所以我的EF環境可能會保持很長一段時間。Ninject刪除/替換InThreadScope對象實例

我想在執行結束時刪除Job的線程的Ninject實例。從而導致它在同一個線程中爲該類型的下一個分辨率創建一個新實例。

如何從Ninject的InThreadScope中移除實例?

+0

爲什麼您必須使用InThreadScope,爲什麼不只是InTransientScope,對於EF環境? – 2015-02-23 15:05:04

+0

正在使用的後臺任務包含許多單獨的步驟,並非所有步驟都將上下文從一個傳遞到另一個。但是他們通過實體(以避免大量的數據庫調用)。 – 2015-02-23 15:26:43

回答

0

遲髮型可以通知你的作業執行(前),並通過實現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