Asynchronous HttpContext.Current implements the method of fetching values (resolving asynchronous Application,session,cache ... such as failure) _ Practical skills

Source: Internet
Author: User
Tags httpcontext
The answer is also most are: Quote system.web, do not use HttpContext.Current.Application should use System.Web.HttpContext.Current.Application, Later on the Internet to see an article on the System.Runtime.Remoting.Messaging.CallContext this class of detailed introduction to know, Originally HttpContext.Current is based on the System.Runtime.Remoting.Messaging.CallContext class, neither child threads nor asynchronous threads can access the data saved by the main thread in CallContext. Therefore, the process of executing asynchronously will result in a httpcontext.current null situation, in order to solve the thread can get httpcontext.current data of the main thread, Need to be in the asynchronous front to save the httpcontext.current in a HttpContext way, and then pass the parameters of the form passed in, the following see the implementation of the method:
Copy Code code as follows:

Public HttpContext context
{
get {return httpcontext.current;}
set {value = context;}
}

Then create a delegate
Copy Code code as follows:

Public delegate String Delegategetresult (HttpContext context);

The following is the coding of the implementation process
Copy Code code as follows:

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 "Congratulations, the jackpot";
}
Else
{
Return "I guess you're going to win the lottery";
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.