基于自定义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();
}
相关文章
强烈推荐一个基于.Net Framework开发的Windows右键菜单管理工具
这篇文章主要介绍了推荐一个基于.Net Framework开发的Windows右键菜单管理工具,今天给大家推荐一个Windows右键菜单管理工具,方便我们管理我们的右键菜单,需要的朋友可以参考下2023-05-05ASP.NET笔记之 Request 、Response 与Server的使用
本篇文章小编为大家介绍,ASP.NET笔记之 Request 、Response 与Server的使用。需要的朋友参考下2013-04-04.NET性能优化之为结构体数组使用StructLinq的问题解析
这篇文章主要介绍了.NET性能优化为结构体数组使用StructLinq,本系列的主要目的是告诉大家在遇到性能问题时,有哪些方案可以去优化;并不是要求大家一开始就使用这些方案来提升性能,需要的朋友可以参考下2022-05-05C#精髓 GridView72大绝技 学习gridview的朋友必看
C#精髓 GridView72大绝技 学习gridview的朋友可以参考下。2009-10-10.NET Core配置TLS Cipher(套件)的详细过程
本文以.NET 5为例,只不过针对.NET Core 3或3.1通过工具扫描出的协议套件结果略有所差异,但不影响我们对安全套件的配置,我们使用OpenSSL生成自签名证书,对.NET Core配置TLS Cipher相关知识感兴趣的朋友一起看看吧2021-12-12.Net Core中使用ExceptionFilter过滤器的方法
这篇文章主要介绍了.Net Core中使用ExceptionFilter过滤器的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-03-03
最新评论