网络编程 
首页 > 网络编程 > 浏览文章

基于自定义Unity生存期模型PerCallContextLifeTimeManager的问题

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

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的问题

上一篇:asp.net C#实现下载文件的六种方法实例
下一篇:关于WPF使用MultiConverter控制Button状态的详细介绍
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网