【发布时间】:2015-03-05 17:39:25
【问题描述】:
我有一个常用的方法来处理可能从多个函数返回的特定错误:
protected async Task<T> RunMyMethod<T>(Func<T> method)
{
try
{
var returnValue = await Task.Run<T>(method);
return returnValue;
}
catch (MyCustomException)
{
// Force a clean shutdown of the software
ShutdownApplication();
return default(T);
}
}
这是一个如何在派生类中使用它的示例:
private async Task<IEnumerable<MyData>> GetMyData()
{
var returnValue = await base.RunMyMethod<IEnumerable<MyData>>(() =>
{
var returnval = GetMyDataFromServer();
return returnval;
});
return returnValue;
}
当MyCustomException 类型的异常发生在GetMyDataFromServer() 中时,软件不会进入catch 块。我在函数GetMyData() 中收到以下错误:
An exception of type 'System.ServiceModel.FaultException`1' occurred in mscorlib.dll but was not handled in user code
Additional information: Exception of type 'MyCustomException' was thrown.
这是仅启用了用户未处理的异常。
GetMyDataFromServer() 与 WCF 服务通信。该服务是引发错误的原因。
ChannelFactory<TChannel> cf = new ChannelFactory<TChannel>(endPointName);
Binding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
var clientCredentials = new ClientCredentials();
. . .
channel = cf.CreateChannel();
var data = channel.CallWCFService();
在网上看了一圈,看来处理这个问题的正确方法是改变基本方法如下:
protected async Task<T> RunMyMethod<T>(Func<T> method)
{
var returnValue = await Task.Run<T>(method).ContinueWith(e =>
{
ShutdownApplication();
return default(T);
}, TaskContinuationOptions.OnlyOnFaulted);
return returnValue;
}
当我运行这个时,我显然没有捕捉到正确的错误消息,但我只是得到一个TaskCancellationException。
所以,我有两个问题:我关于如何处理此异常的结论是否正确,如果是,我如何捕获特定错误;为什么我会收到TaskCancellationException?
【问题讨论】:
-
RunMyMethod版本 1 中的代码应该可以按预期工作。我的意思是如果GetMyDataFromServer抛出MyCustomException,它应该捕获MyCustomException并调用ShutdownApplication。你能展示GetMyDataFromServer的实现吗?或者你可以发一个short but complete sample 吗? -
你能添加
GetMyDataFromServer的代码吗? -
就本期而言,
GetMyDataFromServer()只是一个抛出新的MyCustomException的函数 -
@pm_2 在这种情况下
RunMyMethod会抓住它。 -
我已经更新了问题;虽然它似乎没有
标签: c# error-handling task-parallel-library async-await task