Handling Exceptions in an IHttpAsyncHandler Page

Discussion in 'ASP .Net' started by =?Utf-8?B?QnJhZCBRdWlubg==?=, Apr 11, 2005.

  1. 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
    =?Utf-8?B?QnJhZCBRdWlubg==?=, Apr 11, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. buzz
    Replies:
    1
    Views:
    749
    Scott Allen
    Nov 23, 2004
  2. buzz
    Replies:
    0
    Views:
    589
  3. Nikos Konstas
    Replies:
    1
    Views:
    392
    John Saunders [MVP]
    Jul 11, 2007
  4. UL-Tomten
    Replies:
    0
    Views:
    542
    UL-Tomten
    Dec 9, 2007
  5. Alphapage

    IHttpAsyncHandler problem

    Alphapage, Feb 18, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    3,527
    Alphapage
    Feb 19, 2008
Loading...

Share This Page