我从不了解 WCF 的一件事是为什么当服务器遇到未处理的异常时没有异常消息详细信息传播回调用客户端。
例如,如果我有以下服务器代码
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class Server : IServer
{
public DTO GetDTO()
{
DTO dto = new DTO();
dto.dto = dto;
return dto;
}
}
public class DTO
{
public DTO dto;
}
[ServiceContract]
public interface IServer
{
[OperationContract]
DTO GetDTO();
}
我特意引入了一个ObjectGraph,以便在返回DTO对象时引发序列化异常。
如果我有一个调用此服务器GetDTO()
方法的客户端,我将得到以下CommunicationException
.
套接字连接被中止。这可能是由于处理您的消息时出错或远程主机超出接收超时,或者是潜在的网络资源问题造成的。本地套接字超时为“00:00:58.9350000”。
这是绝对没用的。它没有内部异常,甚至没有真正的异常消息。
如果您随后使用 Microsoft Service TraceViewer,您将看到异常,但您必须为此打开诊断跟踪。
应该发回的异常消息是
尝试序列化参数 http://tempuri.org/:GetDTOResult时出错。InnerException 消息是“'TestWCFLib.DTO' 类型的对象图包含循环,如果禁用引用跟踪,则无法序列化。”。有关更多详细信息,请参阅 InnerException。
那么谁能告诉我如何在客户端显示正确的异常消息?显然,设置IncludeExceptionDetailInFaults
为 true 并没有什么不同。