最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2013年04月18日 11:04:35   作者:  
本篇文章小編將為大家介紹,基于自定義Unity生存期模型PerCallContextLifeTimeManager的問題。需要的朋友參考下

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

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

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

解決辦法有兩種:

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

復(fù)制代碼 代碼如下:

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);
      }
    }


使用舉例
復(fù)制代碼 代碼如下:

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();
    }

相關(guān)文章

最新評論

拜城县| 汶上县| 扎鲁特旗| 策勒县| 都安| 阿拉善右旗| 温宿县| 紫云| 沧州市| 黄山市| 胶南市| 宁远县| 康平县| 富平县| 湘潭市| 望都县| 苍溪县| 翁牛特旗| 无极县| 宁乡县| 白城市| 镇赉县| 阜南县| 临泽县| 波密县| 开远市| 大石桥市| 揭西县| 嘉禾县| 崇左市| 西宁市| 基隆市| 景德镇市| 方山县| 南汇区| 广州市| 玉山县| 裕民县| 梁河县| 长顺县| 江口县|