国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

基于自定義Unity生存期模型PerCallContextLifeTimeManager的問題

2024-07-10 13:16:39
字體:
供稿:網(wǎng)友

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

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

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

解決辦法有兩種:

1.繼續(xù)使用PerThreadLifetimeManager模型,不適用ThreadPool,而手動創(chuàng)建和銷毀線程。
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的問題

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 德格县| 姚安县| 澄江县| 大名县| 民县| 新郑市| 连山| 赫章县| 横峰县| 通辽市| 木兰县| 余干县| 新乡县| 罗源县| 井研县| 高密市| 伊春市| 五常市| 深州市| 波密县| 和静县| 龙岩市| 甘谷县| 清河县| 永新县| 古浪县| 剑阁县| 渭南市| 湖北省| 太仆寺旗| 沈丘县| 光山县| 东乌珠穆沁旗| 泰顺县| 枣强县| 西峡县| 边坝县| 永清县| 和田县| 南开区| 亳州市|