ASP.NET Asynchronous request callback - asp.net

If I call a WebService Async using ASP.NET will the callback code still run even if the user is no longer physically on the page, or when the user leaves the page will the process terminate ?

Yes, if the user close the connection with the server, and your processing takes too much time, you get a throw of "connection close" and your process will terminate.
If you do not like that and you wish to keep the processing make a new thread that is not depend from the connection with the user, and synchronize it with the user return. If the user close the connection then the thread will finish normally, just you not show the results.

Related

Asynchronous ADO command execution, how to close the connection?

The webapp which I’m working on does some data manipulation at the back-end after a user clicks button. This process takes a long time to complete causing the browser to timeout. Therefore I’ve introduced an asynchronous ADO command which causes the page response immediately while the back-end process keeps running. That page also includes an AJAX call to check the status of the back-end process and when it detects that it is completed another AJAX request gets the result of that process form the back-end. All works as expected.
My question is regarding the ADO connection for this scenario as with the asynchronous execution the connection must not be closed.
Is there a way that I can reference the same connection object from another page (the result page requested by AJAX call) and close it ? Or should I just leave it for the server to kill it off eventually.
I was researching a bit for this answer with no success.
Is there a way that I can reference the same connection object from
another page
How about saving async connection in Session("xxx") variable?

Starting a thread in an ASP.NET WebService

I have an IIS hosted WCF webservice.
It has a method on it (let's call it "ConfirmOrder"). When this method is called, I want to
1. Do some quick stuff to the database, resulting in an OrderId
2. Start a new thread that will do some slow work (e.g. generate an email and send it)
3. Return the OrderId from 1. synchronously to the client.
4. Eventually, when it's finished, the new thread created in 2. will have done all the rest of the processing and sent the email.
Questions:
(1) I did have code like:
// do printing and other tasks
OrderConfirmedThreadHelper helper = new OrderConfirmedThreadHelper(userSession, result);
// some things first (like generating barcodes) in this thread
Logger.Write(basket.SessionId, String.Format("Before ConfirmOrderSync"), LogCategoryEnum.Sales, System.Diagnostics.TraceEventType.Verbose);
helper.ConfirmOrderSync();
Logger.Write(basket.SessionId, String.Format("After ConfirmOrderSync"), LogCategoryEnum.Sales, System.Diagnostics.TraceEventType.Verbose);
// slower things (like rendering, sending email) in a separate thread
Thread helperThread = new Thread(new ThreadStart(helper.ConfirmOrderAsync));
helperThread.Start();
return result;
but it seemed to cause problems; at least, the service kept locking up. Is this a bad thing to do?
(2) I tried changing it to
// slower things (like rendering, sending email) in a separate thread
ThreadPool.QueueUserWorkItem(new WaitCallback(helper.ConfirmOrderAsync));
but the ThreadPool thread seems to be being killed as soon as the main thread has finished, because it's a Background thread.
Is there a better way of doing this - short of writing a whole new windows service to communicate with?
If the second thead finishes after the request thread (the one that comes from the browser) you're in problems, since it'll get reclaimed by the runtime and terminated.
If you can afford to wait (if it's only going to send an email i'll be a couple of seconds) you can use ManualResetEvent to synchronize one thread to wait for the other to finish and clean up gracefully.
If you can't wait, well the best choice in this case for the mail process is one of the following
A Windows Service.
An .ashx you can call from your client code with a jquery ajax call passing the necessary data to send the mail.
A batch job (a scheduled task, a sql server job, etc) that reads pending mails to be sent from the DB and sends them. It would run every X minutes, so you wouldn't have to worry
Hope that helps!

asynch process in asp.net

I am migrating an app written on asp.net 1.1. There is a process which can take 5 minutes on one page, processing data in SQL, and letting the user know when it's complete.
To get around the HTTP page timeout, the process runs asynchronously and the page refreshes every 5 seconds checking for completion. It's very simple. Here is the problem: I use a session variable as a semaphore to signal process completion.
This is not working now as I cannot read the semaphore set in the asynch process. The asynch process can read the session from the calling routine, but cannot write back.
First, is there a way to get the asynch process to write to a session variable which can be read by another process? This probably is not the best approach today, but getting the app working is my biggest priority.
Second, if I rewrite it, what approach should be used? This is an asp web app. Not MVC.
use callback technologie it allow you to query an operation server side from your client and get a return from server so no session to manage any more:
http://msdn.microsoft.com/en-us/library/ms178210(v=vs.80).aspx

How to send status information from a Web service while is being executed?

I'm new to web development so I'm not sure what's the best option for the problem that I'm having.
Basically I have a web application that calls a web service for processing some data.
This process may take a long time (hours) and I would to know if there is an easy way to send some status information to the client from time to time.
Right now, the client makes the request from the browser and it just waits there until it finishes.
How can I send some information from the web service? I would like to send a percentage and some additional text specifying what is being done.
Thanks
WCF services can be marked as [OneWay] so that they don't return a response.
or, you could have the service kick off the process in an async manner and then just return to the client that the process has/or hasn't kicked off.
Then, the client can poll another method as the other user has suggested.
If you process takes hours you definitely can't use a sync service because you'll hit your execution timeout or rather the connection timeout for the client.
Maybe you can poll another method for status?
If I were you, I would make the original request asynchronous, as in instead of waiting for the response, it just "starts" the task and returns immediately. Then I would have a separate method on your web service that the app can poll periodically to get the status of the job. once it completes, it can display the data like the original request was doing.
if you want to do it synchronously, you can turn off Response.Buffer and write directly to the response.

how to synchronize webservice calls in javascript

I have a control application - using asp.net webservices.
I have a timer which does an asynchronous webservice call every 5 seconds to update the screen.There are also user buttons to refresh parts of the screen, also doing async webservice calls.
The issue is my screen get messy if a user webservice command is issued before the timer webservice response has arrived.
Basically, I want the user command to wait, if there is a pending async timer call in process. How do I synchronize between these calls in Javascript?
Thanks.
Is it possible to ignore the timed postback when a user postback occurs?
If so, set 2 flags (DontRequest and IgnoreNext) when the user does postback. When the user postback returns, unset DontRequest.
Your automated process would not start requests while DontRequest is set. If a response is received and IgnoreNext is set, ignore the response and unset the flag.

Resources