Note that there are some explanatory texts on larger screens.

plurals
  1. PORunning an asynchronous operation triggered by an ASP.NET web page request
    primarykey
    data
    text
    <p>I have an asynchronous operation that for various reasons needs to be triggered using an HTTP call to an ASP.NET web page. When my page is requested, it should start this operation and immediately return an acknowledgment to the client.</p> <p>This method is also exposed via a WCF web service, and it works perfectly.</p> <p>On my first attempt, an exception was thrown, telling me:</p> <pre>Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.</pre> <p>So of course I added the <code>Async="true"</code> parameter to the <code>@Page</code> directive. Now, I'm not getting an error, but the page is blocking until the Asynchronous operation completes.</p> <p>How do I get a true fire-and-forget page working?</p> <p><strong>Edit:</strong> Some code for more info. It's a bit more complicated than this, but I've tried to get the general idea in there.</p> <pre><code>public partial class SendMessagePage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string message = Request.QueryString["Message"]; string clientId = Request.QueryString["ClientId"]; AsyncMessageSender sender = new AsyncMessageSender(clientId, message); sender.Start(); Response.Write("Success"); } } </code></pre> <p>The AsyncMessageSender class:</p> <pre><code>public class AsyncMessageSender { private BackgroundWorker backgroundWorker; private string client; private string msg; public AsyncMessageSender(string clientId, string message) { this.client = clientId; this.msg = message; // setup background thread to listen backgroundThread = new BackgroundWorker(); backgroundThread.WorkerSupportsCancellation = true; backgroundThread.DoWork += new DoWorkEventHandler(backgroundThread_DoWork); } public void Start() { backgroundThread.RunWorkerAsync(); } ... // after that it's pretty predictable } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload