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 n开发者_如何学Goot found a solution.
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.
I recommend to check if you you have access to Internet before you fire any peace of code. Just check if there is net connection and notifi the user if not.
I have exactly the same problem. The behaviour is different from Desktop-Silverlight where the exception is handled, to WindowsPhone-Silverlight where connection exceptions are not handled at all.
I created a topic here : http://forums.create.msdn.com/forums/t/69485.aspx
Nobody has any real answer. Ah, yes, there is a trick : you can manually modify the reference.cs in order to manually catch the exception.... But if you have lot of functions, it becomes a nightmare.
Maybe Microsoft developers didn't test all the connection usecases, and they forget to implement this one (which is a big one).
精彩评论