亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

多線程應用程序中基于上下文的依賴注入

多線程應用程序中基于上下文的依賴注入

C#
飲歌長嘯 2022-12-24 12:14:55
    我有一個在服務器上運行的服務,它偵聽消息隊列。收到消息時,將啟動一個新線程并將消息傳遞給該線程進行處理。我已經定義了一個接口,它提供對當前用戶的訪問,以便在用于消息處理的各種類中使用:public interface IUserContext {    User CurrentUser { get; }}該用戶可能會從消息更改為消息。我的問題是如何在 SimpleInjector 中注冊 IUserContext 的實現,以便 CurrentUser 屬性正確返回傳入消息中包含的正確用戶?在我的 Asp.Net 應用程序中,這是通過以下方式完成的:container.Register<IUserContext>(() => {    User user = null;    try {         user = HttpContext.Current?.Session[USER_CONTEXT] as IUser;    }    catch { }    return new UserContext(user);});我想這將使用生命周期范圍來完成,但我不能在靜態類中定義它并在每個線程中設置用戶,因為它可能會破壞另一個進程。這是我對實施的最佳猜測? public static Func<User> UserContext { get; set; }然后在我的新線程中的代碼中:using (container.BeginLifetimeScope()) {    .....    var user = GetUserContext(message);    UserContextInitializer.UserContext = () => new UserContext(user);    .....}然后注冊看起來像這樣:container.Register<IUserContext>(() => UserContextInitializer.UserContext);除了線程安全之外,這是在 SimpleInjector 中實現它的正確方法嗎?還有另一種模式會更正確嗎?
查看完整描述

1 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

讓我們從特定于 ASP.NET 的IUserContext注冊開始:


container.Register<IUserContext>(() => {

    User user = null;

    try {

         user = HttpContext.Current?.Session[USER_CONTEXT] as IUser;

    }

    catch { }


    return new UserContext(user);

});

這種注冊是有問題的,因為UserContext組件依賴于運行時數據的可用性,而正如此處所述,對象圖的創建應該與運行時數據分開,并且運行時數據應該流經系統。


換句話說,您應該將您的UserContext課程重寫為以下內容:


public class AspNetUserContext : IUserContext

{

    User CurrentUser => (User)HttpContext.Current.Session[USER_CONTEXT];

}

這允許IUserContext按如下方式注冊此特定于 ASP.NET 的實現:


container.RegisterInstance<IUserContext>(new AspNetUserContext());

當然,前面的并沒有解決你的Windows Service中的問題,但是前面的確實為解決這個問題打下了基礎。


對于 Windows 服務,您還需要自定義實現(適配器):


public class ServiceUserContext : IUserContext

{

    User CurrentUser { get; set; }

}

這個實現要簡單得多,這里ServiceUserContext的CurrentUser屬性是一個可寫屬性。這優雅地解決了您的問題,因為您現在可以執行以下操作:


// Windows Service Registration:

container.Register<IUserContext, ServiceUserContext>(Lifestyle.Scoped);

container.Register<ServiceUserContext>(Lifestyle.Scoped);


// Code in the new Thread:

using (container.BeginLifetimeScope())

{

    .....

    var userContext = container.GetInstance<ServiceUserContext>();


    // Set the user of the scoped ServiceUserContext

    userContext.CurrentUser = GetUserContext(message);


    var handler = container.GetInstance<IHandleMessages<SomeMessage>>();


    handler.Handle(message);


    .....

}

在這里,解決方案也是將對象圖的創建與運行時數據的使用分開。在這種情況下,運行時數據在構造(即使用userContext.CurrentUser = GetUserContext(message))后提供給對象圖。


查看完整回答
反對 回復 2022-12-24
  • 1 回答
  • 0 關注
  • 83 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號