gpt4 book ai didi

c# - AggregateException 和 WCF

转载 作者:太空狗 更新时间:2023-10-29 21:59:21 24 4
gpt4 key购买 nike

我正在调用一个 WCF 服务,它在某些情况下会返回一个 AggregateException,以及通过调用发生的所有问题

另一方面,我收到 FaultException(这是有道理的,因为 WCF 只了解这些异常)。问题是,契约(Contract)的详细信息不是聚合异常。就好像默认情况下,WCF 获取 AggregateException 异常列表 (InnerExceptions) 的第一个异常,并将其封装。所以在客户端,我只是得到列表中的第一个异常(exception)。经过一番调查,我做了以下事情:

将此添加到契约(Contract)中

[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;
}

它进入了 Exception catch 语句,并得到这样的错误(这显然是一些随机错误,因为我没有任何连接问题,并且调试立即返回,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

我错过了什么?

最佳答案

坏处是,你的故障细节源自异常。阅读Using custom FaultContract object containing System.Exception causes 'Add Service Reference' to fail – 姆欧。例如

         [DataContract] 
public class AggregateFault
{
[DataMember]
public string Message { get; set; }
}

然后 FaultException<AggregateFault >工作完美但不是FaultException<AggregateException>

关于c# - AggregateException 和 WCF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6226869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com