Inside my Silverlight app, I m calling a web service asynchronously, using the client generated for me by Visual Studio (Add Service Reference...).
My code looks like this:
ServiceClient serviceClient = new ServiceClient();
serviceClient.GetServerTimeCompleted += new EventHandler<GetServerTimeCompletedEventArgs>(serviceClient_GetServerTimeCompleted);
serviceClient.GetServerTimeAsync();
void serviceClient_GetServerTimeCompleted(object sender, GetServerTimeCompletedEventArgs e)
{
// do nothing atm
}
只要服务正常运行,一切都很顺利,但是如果服务未运行,我会收到:
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
"An exception occurred during the operation, making the result invalid. Check 内部异常 for exception details."
内部异常
"An error occurred while trying to make a request to URI http:// . This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details."
在这些情况下出现异常是有道理的,但我无法弄清楚如何在客户端代码中捕获异常。有没有标准的方法来解决这个问题?如果我理解这是如何工作的,当我的代码调用生成的代码时,它会产生另一个线程,实际上调用Web服务并等待响应。异常在那个线程和生成的代码中。我的调用代码(见上文)和回调方法都没有机会看到异常。
谢谢 (Xièxiè)
基督教