Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows phone 7, silverlight - How to catch EndpointNotFoundException when calling async web service?

I'm calling a web service from within a Silverlight Windows phone 7 application which works fine if the web service is available. If the web service is unavailable then an unhandled exception is caught but I don't seem able to catch it myself. My attempt is as follows:

            //Get list of providers from Webservice
        RSAServiceClient proxy = new RSAServiceClient();
        proxy.GetSuppliersCompleted += new System.EventHandler<GetSuppliersCompletedEventArgs>(ProxyGetSuppliersCompleted);
        try
        {
            proxy.GetSuppliersAsync();
        }
        catch (EndpointNotFoundException)
        {
            //TODO: Handle webserice not being available
        }
        catch (Exception)
        {
            throw;
        }

But this doesn't catch the exception and obviously GetSuppliersCompleted never get's called so I can't catch it there.

I then thought I may be able to detect it by checking the connection state (proxy.State) but this despite the web service not running returns CommunicationState.Opened.

Any idea's where I can handle this?

Apologies if I've missed something but I have searched and not found a solution.

like image 701
Fishcake Avatar asked Nov 26 '10 13:11

Fishcake


1 Answers

You should place your catch in your ProxyGetSuppliersCompleted method.

An attempt to access the result will throw the error you are expecting.

Alternatively in the ProxyGetSuppliersCompleted you can test the Error property of the EventArgs which will contain the exception.

like image 145
AnthonyWJones Avatar answered Oct 17 '22 14:10

AnthonyWJones