溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》
  • 首頁 > 
  • 教程 > 
  • 開發技術 > 
  • 如何理解基于自定義Unity生存期模型PerCallContextLifeTimeManager的問題

如何理解基于自定義Unity生存期模型PerCallContextLifeTimeManager的問題

發布時間:2021-10-23 17:40:59 來源:億速云 閱讀:101 作者:iii 欄目:開發技術

這篇文章主要介紹“如何理解基于自定義Unity生存期模型PerCallContextLifeTimeManager的問題”,在日常操作中,相信很多人在如何理解基于自定義Unity生存期模型PerCallContextLifeTimeManager的問題問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何理解基于自定義Unity生存期模型PerCallContextLifeTimeManager的問題”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

PerThreadLifetimeManager的問題
使用Unity內置的PerThreadLifetimeManager生存期模型時,其基于ThreadStatic的TLS(Thread Local Storage)設計,也就是說對于每個托管的ManagedThreadId,其會緩存已生成的對象實例。

由于CLR維護了托管線程池,使用過的線程并不會立即銷毀,在需要的時候會繼續復用。在類似ASP.NET PerCall或WCF PerCall條件下,當Call1在線程ManagedThreadId1中處理完畢后,Call2發生,而Call2很有可能也在線程ManagedThreadId1中處理。這種條件下Call2會自動復用處理Call1時生成并緩存的對象實例。

如果我們希望每次調用(PerCall)都生成專用的對象實例,則PerThreadLifetimeManager在此種場景下不適合。

解決辦法有兩種:

1.繼續使用PerThreadLifetimeManager模型,不適用ThreadPool,而手動創建和銷毀線程。
2.自定義對象生存期模型
PerCallContextLifeTimeManager

復制代碼 代碼如下:


public class PerCallContextLifeTimeManager : LifetimeManager
    {
      private string _key =
        string.Format(CultureInfo.InvariantCulture,
        "PerCallContextLifeTimeManager_{0}", Guid.NewGuid());

      public override object GetValue()
      {
        return CallContext.GetData(_key);
      }

      public override void SetValue(object newValue)
      {
        CallContext.SetData(_key, newValue);
      }

      public override void RemoveValue()
      {
        CallContext.FreeNamedDataSlot(_key);
      }
    }

使用舉例

復制代碼 代碼如下:


private static void TestPerCallContextLifeTimeManager()
    {
      IExample example;
      using (IUnityContainer container = new UnityContainer())
      {
        container.RegisterType(typeof(IExample), typeof(Example),
          new PerCallContextLifeTimeManager());

        container.Resolve<IExample>().SayHello();
        container.Resolve<IExample>().SayHello();

        Action<int> action = delegate(int sleep)
        {
          container.Resolve<IExample>().SayHello();
          Thread.Sleep(sleep);
          container.Resolve<IExample>().SayHello();
        };

        Thread thread1 = new Thread((a) => action.Invoke((int)a));
        Thread thread2 = new Thread((a) => action.Invoke((int)a));
        thread1.Start(50);
        thread2.Start(55);
        thread1.Join();
        thread2.Join();

        ThreadPool.QueueUserWorkItem((a) => action.Invoke((int)a), 50);
        ThreadPool.QueueUserWorkItem((a) => action.Invoke((int)a), 55);
        Thread.Sleep(100);

        ThreadPool.QueueUserWorkItem((a) => action.Invoke((int)a), 50);
        ThreadPool.QueueUserWorkItem((a) => action.Invoke((int)a), 55);
        Thread.Sleep(100);

        ThreadPool.QueueUserWorkItem((a) => action.Invoke((int)a), 50);
        ThreadPool.QueueUserWorkItem((a) => action.Invoke((int)a), 55);
        Thread.Sleep(100);

        example = container.Resolve<IExample>();
      }

      example.SayHello();

      Console.ReadKey();
    }

如何理解基于自定義Unity生存期模型PerCallContextLifeTimeManager的問題

到此,關于“如何理解基于自定義Unity生存期模型PerCallContextLifeTimeManager的問題”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女