Handling Exceptions in an IHttpAsyncHandler Page

G

Guest

I have a question about the correct way to handle exceptions. Most of the
sample code I have seen doesn't address this.

My BeginProcessRequest looks like any other.

public IAsyncResult BeginProcessRequest(HttpContext ctx,AsyncCallback
cb,object obj)
{
AsyncRequestState reqState = new AsyncRequestState(ctx, cb, obj);
_threadPool.PostRequest(new WorkRequestDelegate(this.AsyncProcessRequest),
reqState);
return reqState;
}

The first difference is in AsyncProcessRequest, where I put a try/catch
block around the call to ProcessRequest.

void AsyncProcessRequest(object state, DateTime requestTime)
{
AsyncRequestState reqState = state as AsyncRequestState;
try {
base.ProcessRequest(reqState._ctx);
} catch(Exception e) {
reqState.Exception = e;
}
reqState.CompleteRequest();
}

I have also added an Exception property to my AsyncRequestState.

And finally I re-throw the exception in EndProcessRequest.

public void EndProcessRequest( IAsyncResult ar )
{
if ( ar != null ) {
AsyncRequestState reqState = ar as AsyncRequestState;
if ( reqState != null && reqState.Exception != null ) {
throw reqState.Exception;
}
}
}

Is there anything wrong with this strategy? I use remoting heavily and
occasionnaly get bad behavior when running asynchronously.

Thanks,
Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top