基于自定義Unity生存期模型PerCallContextLifeTimeManager的問(wèn)題_.Net教程
推薦:asp.net C#實(shí)現(xiàn)下載文件的六種方法實(shí)例asp.net C#實(shí)現(xiàn)下載文件的六種方法實(shí)例,需要的朋友可以參考一下
PerThreadLifetimeManager的問(wèn)題
使用Unity內(nèi)置的PerThreadLifetimeManager生存期模型時(shí),其基于ThreadStatic的TLS(Thread Local Storage)設(shè)計(jì),也就是說(shuō)對(duì)于每個(gè)托管的ManagedThreadId,其會(huì)緩存已生成的對(duì)象實(shí)例。
由于CLR維護(hù)了托管線程池,使用過(guò)的線程并不會(huì)立即銷毀,在需要的時(shí)候會(huì)繼續(xù)復(fù)用。在類似ASP.NET PerCall或WCF PerCall條件下,當(dāng)Call1在線程ManagedThreadId1中處理完畢后,Call2發(fā)生,而Call2很有可能也在線程ManagedThreadId1中處理。這種條件下Call2會(huì)自動(dòng)復(fù)用處理Call1時(shí)生成并緩存的對(duì)象實(shí)例。
如果我們希望每次調(diào)用(PerCall)都生成專用的對(duì)象實(shí)例,則PerThreadLifetimeManager在此種場(chǎng)景下不適合。
解決辦法有兩種:
1.繼續(xù)使用PerThreadLifetimeManager模型,不適用ThreadPool,而手動(dòng)創(chuàng)建和銷毀線程。
2.自定義對(duì)象生存期模型
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容器中的對(duì)象生存期管理分析基于Unity容器中的對(duì)象生存期管理分析
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發(fā)送Email實(shí)例(可帶附件)
- js實(shí)現(xiàn)廣告漂浮效果的小例子
- asp.net Repeater 數(shù)據(jù)綁定的具體實(shí)現(xiàn)
- Asp.Net 無(wú)刷新文件上傳并顯示進(jìn)度條的實(shí)現(xiàn)方法及思路
- Asp.net獲取客戶端IP常見(jiàn)代碼存在的偽造IP問(wèn)題探討
- VS2010 水晶報(bào)表的使用方法
- ASP.NET中操作SQL數(shù)據(jù)庫(kù)(連接字符串的配置及獲取)
- asp.net頁(yè)面?zhèn)髦禍y(cè)試實(shí)例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲(chǔ)過(guò)程實(shí)現(xiàn)分頁(yè)示例代碼
.Net教程Rss訂閱編程教程搜索
.Net教程推薦
- ASP.NET中Session丟失原因與解決方案小結(jié)
- 談?wù)凥tmlControl與WebControl的區(qū)別與用途
- ASP.NET中用healthMonitor屬性用法
- ASP.NET網(wǎng)絡(luò)編程中常用到的27個(gè)函數(shù)集
- .NET Framework 3.5 SP1正式版
- 使用ADO.NET2.0提升數(shù)據(jù)交互性能(1)
- ASP.NET里的事務(wù)處理
- 用SQL語(yǔ)句修復(fù)SQL Server數(shù)據(jù)庫(kù)
- C# 如何獲取指定目錄包含的文件和子目錄
- asp.net中使用repeater和PageDataSource搭配實(shí)現(xiàn)分頁(yè)代碼
- 相關(guān)鏈接:
復(fù)制本頁(yè)鏈接| 搜索基于自定義Unity生存期模型PerCallContextLifeTimeManager的問(wèn)題
- 教程說(shuō)明:
.Net教程-基于自定義Unity生存期模型PerCallContextLifeTimeManager的問(wèn)題
。