我正在调用一个WCF服务,它在某些条件下会返回一个AggregateException,其中包含调用过程中发生的所有问题
另一方面,我得到了一个FaultException (这是有意义的,因为WCF只了解这些异常)。问题是,合同的细节并不是一个聚合的例外。这就好像在默认情况下,WCF获取AggregateException异常列表(InnerExceptions)的第一个异常,并将其封装。所以在客户端,我只是得到了列表中的第一个异常。在做了一些调查之后,我做了以下工作:
已将此添加到合同中
[FaultContract(typeof(AggregateException))]然后在服务调用中..
try
{
BaseService.Blabla.Delete(item);
}
catch (AggregateException ex)
{
throw new FaultException<AggregateException>(ex);
} 但是在另一边,这是这样的:
catch (FaultException<AggregateException> ex)
{
string msg = string.Empty;
foreach (var innerException in ex.Detail.InnerExceptions)
{
msg += innerException + Environment.NewLine;
}
MessageBox.Show(msg);
}
catch (Exception ex)
{
throw ex;
}取而代之的是进入异常捕获语句,并得到如下错误(这显然是一些随机错误,因为我没有任何连接问题,并且调试会立即返回,4分钟不会过去):
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:03:59.9939994'. : An existing connection was forcibly closed by the remote host我遗漏了什么?
发布于 2012-04-05 14:33:55
不幸的是,你的错误细节来自于异常。请阅读Using custom FaultContract object containing System.Exception causes 'Add Service Reference' to fail - mho。例如
[DataContract]
public class AggregateFault
{
[DataMember]
public string Message { get; set; }
}那么FaultException<AggregateFault >就完美了,但FaultException<AggregateException>就不行了
发布于 2012-03-21 04:08:07
我怀疑您的问题是在您使用BaseService代码之前出现的,所以您实际上并没有抛出一个AggregateException。你需要确定抛出了什么异常,最简单的方法是在服务器上调试,下一个简单的方法是挂接一些日志。
如果您想要能够轻松地跟踪这些东西,并且能够处理错误等,最好的选择是实现IErrorHandler,我使用的一个基本实现通常是这样的:
public class ErrorHandler : IErrorHandler
{
private readonly Action<Exception> LogException;
private readonly Action<Message> LogFault;
public ErrorHandler(Action<Exception> logException, Action<Message> logFault)
{
LogException = logException;
LogFault = logFault;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (error is FaultException) // Thrown by WCF - eg request deserialization problems, can be explicitly thrown in code
{
LogFault(fault);
return;
}
var faultCode = new FaultCode("UnknownFault");
if (error is ArgumentOutOfRangeException)
{
faultCode = new FaultCode("ArgumentOutOfRange");
}
var action = OperationContext.Current.IncomingMessageHeaders.Action;
fault = Message.CreateMessage(version, faultCode, error.Message, action);
LogFault(fault);
}
public bool HandleError(Exception error)
{
// Logging of exceptions should occur here as all exceptions will hit HandleError, but some will not hit ProvideFault
LogException(error);
return false; // false allows other handlers to be called - if none return true the dispatcher aborts any session and aborts the InstanceContext if the InstanceContextMode is anything other than Single.
}
}注意:上面的代码不能完全迎合你的AggregateException,但会让你走上正确的道路,如果你选择走这条路,你还需要注入错误处理程序。
https://stackoverflow.com/questions/6226869
复制相似问题