Questions during DevDays

While at the DevDays several people came up to me after the sessions. A couple of them had very interesting questions.


Constantijn Enders of LogicaCMG asked me where to catch any exceptions that are thrown during an asynchronous page or task. I responded that it should be in the End… handler for the async operation. We tried to do so after the session, but I must have made an error. It did not seem to work. It should have and it did when I tried it again later. Typically you will need to catch the exceptions that are thrown during an async operation in a try/catch/finally block around the EndInvoke like method. Here’s a sample:


protected void Page_Load(object sender, EventArgs e)
{
  this.PreRenderComplete += newEventHandler

      (LongRunningAsync_PreRenderComplete);
  this
.AddOnPreRenderCompleteAsync(
      newBeginEventHandler
(BeginAsynchronousOperation),
      newEndEventHandler
(EndAsynchronousOperation));
}


void LongRunningAsync_PreRenderComplete(object sender, EventArgs e)
{
  statusLabel.Text = DateTime
.Now.ToLongTimeString();
}


privateIAsyncResult BeginAsynchronousOperation(object sender,
    EventArgs args, AsyncCallback callback, object
state)
{
  return
proxy.BeginDoNothing(callback, state);
}


private void EndAsynchronousOperation(IAsyncResult ar)
{
  // Exception handling of async call is performed here
  try
  {
    proxy.EndDoNothing(ar);
  }
  catch (Exception
ex)
  {
    Response.Write(“An exception was handled: “
+ ex.Message);
  }
}


Notice the bold part, where the exception is caught. It is rethrown at the client side by the generated proxy code after the web service has thrown the exception first.


Another question came from Taco Ditiecher who asked me whether the async tasks could be used from a regular or asynchronous http handler implementation. My best guess was that it could not be done. First hunch: the tasks you must instantiate are object from the PageAsyncTask class. Next, the execution of the Begin and End delegates of the async tasks are tightly coupled to the page lifecycle.


Taco did some more spelunking with Reflector and mailed me to say that indeed it could not be done. He found there is a PageAsyncTaskManager class taking care of things and it requires a Page object in the constructor. So, you’re out of luck there.


No worries though. You can still use a barebones Page derived class as the vehicle to run your async tasks. It is a small price to pay. The overhead is minimal if you do not have a controls in the body in your .aspx file. And you do not need to hook up handlers for the regular events, such as for Init, Load, et cetera.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s