自定义Unity基于PerCall的生存期模型
2013-04-14 16:17
267 查看
PerThreadLifetimeManager的问题
使用Unity内置的PerThreadLifetimeManager生存期模型时,其基于ThreadStatic的TLS(Thread Local Storage)设计,也就是说对于每个托管的ManagedThreadId,其会缓存已生成的对象实例。由于CLR维护了托管线程池,使用过的线程并不会立即销毁,在需要的时候会继续复用。在类似ASP.NET PerCall或WCF PerCall条件下,当Call1在线程ManagedThreadId1中处理完毕后,Call2发生,而Call2很有可能也在线程ManagedThreadId1中处理。这种条件下Call2会自动复用处理Call1时生成并缓存的对象实例。
如果我们希望每次调用(PerCall)都生成专用的对象实例,则PerThreadLifetimeManager在此种场景下不适合。
解决办法有两种:
继续使用PerThreadLifetimeManager模型,不适用ThreadPool,而手动创建和销毁线程。
自定义对象生存期模型
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(); }
![](http://images.cnitblog.com/blog/175043/201304/13171157-e8fa4988df2341f38702e4a113998e25.png)
相关文章推荐
- 基于自定义Unity生存期模型PerCallContextLifeTimeManager的问题
- 基于自定义Unity生存期模型PerCallContextLifeTimeManager的问题
- 自定义Unity生存期模型PerCallContextLifeTimeManager
- Unity Shader自定义光照模型
- (四)Qt实现自定义模型基于QAbstractTableModel (一般)
- C#、Unity网络通信中基于字节码的自定义协议解码,C#版ByteBuffer
- 基于Unity的AOP的符合基于角色的访问控制(RBAC)模型的通用权限设计
- Unity基于组件的对象模型
- 基于 Django1.5自定义user模型
- 第13章 TCP编程(3)_基于自定义协议的多进程模型
- Unity基于组件的对象模型
- unity学习2——基于物理模拟的太阳系模型
- 让IoC动态解析自定义配置(提供基于Unity的实现)
- 第13章 TCP编程(4)_基于自定义协议的多线程模型
- Unity基于组件的对象模型
- Unity Shader自定义光照模型
- 基于Unity容器中的对象生存期管理分析
- 基于 HTML5 的 WebGL 自定义 3D 摄像头监控模型
- 谈我的SiteFactory文章发布方式一个小扩展,从模型、标签、模板到自定义插件,基于ashx的
- 基于 HTML5 的 WebGL 自定义 3D 摄像头监控模型