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
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