Skip to main content
 首页 » 编程设计

c#之如何从 CompletedEventArgs 获取抛出异常的类型

2024年02月20日14thcjp

我使用 C# 和 Visual Studio 2012 开发了一个 WP8 应用程序。我向我的项目添加了服务引用(添加服务引用)。所以我能够使用网络服务功能。

client = new YChatWebService.WebServiceControllerPortTypeClient(); 
 
client.getDataCompleted += client_getDataCompleted; 
client.InnerChannel.OperationTimeout = new TimeSpan(0, 0, 0, 0, 500); 
client.getDataAsync();  
 
void client_getDataCompleted(object sender, getDataCompletedEventArgs e) 
{ 
    // e.Error.Message 
 
} 

我为 getData() 设置了 500 毫秒的超时限制;如果超过时间限制,则会出现以下错误:

"The HTTP request to 'http://example.com/webService/index?ws=1' has exceeded the allotted timeout. The time allotted to this operation may have been a portion of a longer timeout."

这很好 :) 但是,我想知道抛出了什么样的异常。类似于包含字符串“TimeoutException”的字符串变量会很好。我怎样才能做到这一点?

请您参考如下方法:

你可以使用

if( e.Error is TimeoutException ) 

查看Exception是否为TimeoutException类型