I have an asp.net web site that is designed to take a standard URL request and returns XML to the consumer.
Once the request is initiated, our system runs off and does some processing and returns to the requestor after a bit.
We have set it up so that the system spawns a thread to do the processing which can be killed and resulting class information can be captured and returned via the XML response.
This current method is blocking subsequent requests. Although most requests are quick, not all of them are and we need to have the site to handle concurrent requests as any site normally would. I'm looking for an alternative to what we're doing.
Here is the basic framework:
PageLoad()->Create instance of ProcessClass->StartWorkerThread(ProcessClass.Run())->Wait for thread to finish, if thread takes too long, abort->Return XML document(ProcessClass.Results)
An alternative would be to just allow the site to timeout by its normal mechanism, but I haven't found a good way to capture ProcessClass.Results once the site times out and return in a friendly way. Usually, ProcessClass completes normally, but the reponse
is just a generic timeout response. We need to halt the class processing and return a friendly message.
This current method is blocking subsequent requests. Although most requests are quick, not all of them are and we need to have the site to handle concurrent requests as any site normally would. I'm looking for an alternative to what we're doing.
Look into async handlers or async pages. This will free up the thread in the thread pool for other requests. It doesn't help, tho, if all you've done is start another thread in the thread pool.
mtanquary
0 Points
1 Post
Managing asynchronous processing / better method?
Dec 06, 2012 07:45 PM|LINK
I have an asp.net web site that is designed to take a standard URL request and returns XML to the consumer.
Once the request is initiated, our system runs off and does some processing and returns to the requestor after a bit.
We have set it up so that the system spawns a thread to do the processing which can be killed and resulting class information can be captured and returned via the XML response.
This current method is blocking subsequent requests. Although most requests are quick, not all of them are and we need to have the site to handle concurrent requests as any site normally would. I'm looking for an alternative to what we're doing.
Here is the basic framework:
PageLoad()->Create instance of ProcessClass->StartWorkerThread(ProcessClass.Run())->Wait for thread to finish, if thread takes too long, abort->Return XML document(ProcessClass.Results)
An alternative would be to just allow the site to timeout by its normal mechanism, but I haven't found a good way to capture ProcessClass.Results once the site times out and return in a friendly way. Usually, ProcessClass completes normally, but the reponse is just a generic timeout response. We need to halt the class processing and return a friendly message.
Here is the basic code:
protected void Page_Load(object sender, EventArgs e) { ImportRequestFactory irf = new ImportRequestFactory(); if (irf.ValidateWebRequest()) { ImportRequestBase req = irf.CreateImportRequest(); RunWithTimeout(new ParameterizedThreadStart(ImportFile), Int16.Parse(ConfigurationManager.AppSettings["TIMEOUT_IN_MINUTES"]), (ImportMailingRequest)req); Response.Clear(); Response.ContentType = "text/xml"; string xml = req.GetResultsXML(); req.ResponseXML = xml; req.Save(); Response.Write(xml); } else { //Returns error message } } protected void RunWithTimeout(ParameterizedThreadStart threadStart, int maxMinutesProcessing, ImportMailingRequest req) { Thread workerThread = new Thread(threadStart); DateTime start = DateTime.Now; TimeSpan duration; workerThread.Start(req); while (!workerThread.ThreadState.Equals(ThreadState.Stopped)) { duration = DateTime.Now - start; if (duration.TotalMinutes > maxMinutesProcessing) { Logger.Log((int)req.ImportId, Types.LogType.unknown, false, "Aborting import " + req.ImportId + ". It has exceeded the timeout duration."); workerThread.Abort(); } Thread.Sleep(1000); } } protected void ImportFile(object request) { ((ImportMailingRequest)request).ImportFile(((ImportMailingRequest)request).LocalDownloadPath, false); }Thank you!
BrockAllen
All-Star
27524 Points
4902 Posts
MVP
Re: Managing asynchronous processing / better method?
Dec 07, 2012 02:40 AM|LINK
Look into async handlers or async pages. This will free up the thread in the thread pool for other requests. It doesn't help, tho, if all you've done is start another thread in the thread pool.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/