简体   繁体   中英

Handling Exceptions from an Async WCF Web Service call

I have a third-party web-service reference which has a web method AddDocument . Our application called this method synchronously before but I need to implement asynchronous call to this method now.

I have reconfigured web-service reference in my project by checking "Generate Asynchronous Operations" checkbox. Now I have a BeginAddDocument method available, which I am calling as:

AsyncCallback callback = ProcessAsyncResult;
object asyncState = "TestAsyncState";
srv.BeginAddDocument(Credentials, document, callback, asyncState);

And I have prepared a stab for a callback method:

public void ProcessAsyncResult(IAsyncResult result)
{
}

Sometimes the web-service returns errors. They can be easily catched and handled while calling method synchronously.

But unfortunately I cannot find any clear answers how to catch exceptions using asynchronous approach, keeping in mind that I cannot modify the web-service itself.

IAsyncResult in the callback method seems to be the same regardless of the operation result - be it a success or an exception.

Is it possible at all to catch exceptions from a web service using asynchronous approach, and if it is, what is the best way to make it possible?

Usually with the "BeginYYYxxx" ASync pattern, there is a matching "EndYYYxxx" method that you call. Assuming they've written the third-party service using this pattern, you should be able to just get the resulting object from the AsyncState of IAsyncResult. The documentation for IAsyncResult may be of use to you.

Without seeing the documentation for the service, this is my best guess:

AsyncCallback callback = ProcessAsyncResult;
object asyncState = "TestAsyncState";
srv.BeginAddDocument(Credentials, document, callback, asyncState);


public void ProcessAsyncResult(IAsyncResult result)
{
    TypeThatsrvIs s = (TypeThatsrvIs)result.AsyncState;

    //"s" will be the same srv object that you called BeginAddDocument on.
    //It "should" contain the web-service result.
    s = s.EndAddDocument(result);

    /*
    Do stuff with that resulting object.
    */

}

Hope that helps.

Edit: You may actually have to pass the srv object as the AsyncState in order to get it back in the ProcessAsyncResult method:

AsyncCallback callback = ProcessAsyncResult;
object asyncState = srv;
srv.BeginAddDocument(Credentials, document, callback, asyncState);

As Brandon suggested in his reply, the solution was really simple, and was not clear for me only due to my lack of experience in asynchronous web method call pattern. The resulting code of the callback method is as follows:

public void ProcessAsyncResult(IAsyncResult result)
    {
        ServiceSoapClient srv = result.AsyncState as ServiceSoapClient;
        if (srv == null) return;
        try
        {
            srv.EndAddDocument(result);
        }
        catch (WebException ex)
        {
            //your code to handle exception here
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM