色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

異步 HttpContext.Current實(shí)現(xiàn)取值的方法(解決異步Application,Session,Cache...等失效的問(wèn)題)

回答的也多數(shù)都是:引用System.Web,不要用HttpContext.Current.Application應(yīng)該用System.Web.HttpContext.Current.Application,后來(lái)在網(wǎng)上看到一篇關(guān)于System.Runtime.Remoting.Messaging.CallContext這個(gè)類的詳細(xì)介紹才知道,原來(lái)HttpContext.Current是基于System.Runtime.Remoting.Messaging.CallContext這個(gè)類,子線程和異步線程都無(wú)法訪問(wèn)到主線程在CallContext中保存的數(shù)據(jù)。所以在異步執(zhí)行的過(guò)程會(huì)就會(huì)出現(xiàn)HttpContext.Current為null的情況,為了解決子線程能夠得到主線程的HttpContext.Current數(shù)據(jù),需要在異步前面就把HttpContext.Current用HttpContext的方式存起來(lái),然后能過(guò)參數(shù)的形式傳遞進(jìn)去,下面看看實(shí)現(xiàn)的方法:
復(fù)制代碼 代碼如下:
public HttpContext context
{
get { return HttpContext.Current; }
set { value = context; }
}

然后建立一個(gè)委托
復(fù)制代碼 代碼如下:
public delegate string delegategetResult(HttpContext context);

下面就是實(shí)現(xiàn)過(guò)程的編碼
復(fù)制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
context = HttpContext.Current;
delegategetResult dgt = testAsync;
IAsyncResult iar = dgt.BeginInvoke(context, null, null);
string result = dgt.EndInvoke(iar);
Response.Write(result);
}

public static string testAsync(HttpContext context)
{
if (context.Application["boolTTS"] == null)
{
Hashtable ht = (Hashtable)context.Application["TTS"];
if (ht == null)
{
ht = new Hashtable();
}

if (ht["A"] == null)
{
ht.Add("A", "A");
}

if (ht["B"] == null)
{
ht.Add("B", "B");
}

context.Application["TTS"] = ht;
}

Hashtable hts = new Hashtable();
hts = (Hashtable)context.Application["TTS"];
if (hts["A"] != null)
{
return "恭喜,中大獎(jiǎng)呀";
}
else
{
return "我猜你快中獎(jiǎng)了";
}
}

AspNet技術(shù)異步 HttpContext.Current實(shí)現(xiàn)取值的方法(解決異步Application,Session,Cache...等失效的問(wèn)題),轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 凌海市| 小金县| 永顺县| 栾城县| 峨眉山市| 宿州市| 华蓥市| 虞城县| 将乐县| 朝阳区| 鄯善县| 资阳市| 五大连池市| 吉水县| 炎陵县| 岳阳县| 尼木县| 明光市| 沙坪坝区| 叙永县| 威远县| 烟台市| 郯城县| 广安市| 漳平市| 探索| 绿春县| 万源市| 桐乡市| 呈贡县| 屏南县| 英山县| 鸡西市| 安泽县| 扶风县| 和硕县| 扶绥县| 白河县| 教育| 延安市| 从化市|