異步 HttpContext.Current實現(xiàn)取值的方法(解決異步Application,Session,Cache...等失效的問題)
更新時間:2009年07月18日 14:22:12 作者:
在一個項目中,為了系統(tǒng)執(zhí)行效率更快,把一個經(jīng)常用到的數(shù)據(jù)庫表通過dataset放到Application中,發(fā)現(xiàn)在異步實現(xiàn)中每一次都會出現(xiàn)HttpContext.Current為null的異常,后來在網(wǎng)上查了好多資料,發(fā)現(xiàn)問這個問題的人多,回答的少
回答的也多數(shù)都是:引用System.Web,不要用HttpContext.Current.Application應該用System.Web.HttpContext.Current.Application,后來在網(wǎng)上看到一篇關(guān)于System.Runtime.Remoting.Messaging.CallContext這個類的詳細介紹才知道,原來HttpContext.Current是基于System.Runtime.Remoting.Messaging.CallContext這個類,子線程和異步線程都無法訪問到主線程在CallContext中保存的數(shù)據(jù)。所以在異步執(zhí)行的過程會就會出現(xiàn)HttpContext.Current為null的情況,為了解決子線程能夠得到主線程的HttpContext.Current數(shù)據(jù),需要在異步前面就把HttpContext.Current用HttpContext的方式存起來,然后能過參數(shù)的形式傳遞進去,下面看看實現(xiàn)的方法:
public HttpContext context
{
get { return HttpContext.Current; }
set { value = context; }
}
然后建立一個委托
public delegate string delegategetResult(HttpContext context);
下面就是實現(xiàn)過程的編碼
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 "恭喜,中大獎呀";
}
else
{
return "我猜你快中獎了";
}
}
復制代碼 代碼如下:
public HttpContext context
{
get { return HttpContext.Current; }
set { value = context; }
}
然后建立一個委托
復制代碼 代碼如下:
public delegate string delegategetResult(HttpContext context);
下面就是實現(xiàn)過程的編碼
復制代碼 代碼如下:
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 "恭喜,中大獎呀";
}
else
{
return "我猜你快中獎了";
}
}
相關(guān)文章
ASP.NET Core 配置和使用環(huán)境變量的實現(xiàn)
這篇文章主要介紹了ASP.NET Core 配置和使用環(huán)境變量的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08form身份驗證通過后,只能用FormsAuthentication.RedirectFromLoginPage
form身份驗證通過后,只能用FormsAuthentication.RedirectFromLoginPage2009-03-03如何給ASP.NET Core Web發(fā)布包做減法詳解
在ASP.Net中可以使用打包與壓縮這兩種技術(shù)來提高Web應用程序頁面加載的性能。下面這篇文章主要給大家介紹了關(guān)于如何給ASP.NET Core Web發(fā)布包做減法的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧2018-06-06asp.net使用母版頁中使用ajax腳本取數(shù)據(jù)
因母版頁繼承自UserControl,我們無法像正常頁面那樣使用Jquey或Ajax的PageMethods等無刷新方法取數(shù)據(jù)。不過可以使用ajax提供的Sys.Net.WebRequest來解決這一問題。2010-09-09在 ASP.Net Core 中使用 MiniProfiler的方法
這篇文章主要介紹了在 ASP.Net Core 中使用 MiniProfiler的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03