没有异常消息详细信息的 WCF CommunicationException [英] WCF CommunicationException with no Exception message details

查看:27
本文介绍了没有异常消息详细信息的 WCF CommunicationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从不了解 WCF 的一件事是,当服务器遇到未处理的异常时,为什么没有异常消息详细信息传播回调用客户端.

One of the things I never understood about WCF is why no Exception message details are propagated back to the calling client when the server encounters an unhandled exception.

例如,如果我有以下服务器代码

For example, if I have the following server code

[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对象时引发序列化异常.

I deliberately introduced an ObjectGraph to cause a serialization exception when the DTO object is returned.

如果我有一个客户端调用此服务器的 GetDTO() 方法,我将得到以下 CommunicationException.

If I have a client that calls this Server's GetDTO() method, I will get the following CommunicationException.

套接字连接已中止.这可能是由错误引起的处理您的消息或接收超时被超过远程主机,或底层网络资源问题.本地套接字超时为00:00:58.9350000".

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:00:58.9350000'.

这绝对没用.它没有内部异常,甚至没有真正的异常消息.

Which is absolutely useless. It has no inner exception and not even the real exception message.

如果您随后使用 Microsoft Service TraceViewer,您将看到异常,但您必须为此打开诊断跟踪.

If you then use Microsoft Service TraceViewer, you will see the exception but you must turn on the Diagnostics tracing for this.

应该返回的异常消息是

尝试序列化参数时出错http://tempuri.org/:GetDTOResult.InnerException 消息是'TestWCFLib.DTO' 类型的对象图包含循环并且不能如果参考跟踪被禁用,则序列化.'.请参见InnerException 了解更多详情.

There was an error while trying to serialize parameter http://tempuri.org/:GetDTOResult. The InnerException message was 'Object graph for type 'TestWCFLib.DTO' contains cycles and cannot be serialized if reference tracking is disabled.'. Please see InnerException for more details.

那么谁能告诉我如何在客户端显示正确的异常消息?显然,将 IncludeExceptionDetailInFaults 设置为 true 并没有什么区别.

So can anybody tell me how get the right exception message show up on the client side? Obviously, setting IncludeExceptionDetailInFaults to true doesn't make a difference.

推荐答案

我认为服务器错误不会传播到客户端是设计使然.这通常是一种不向客户端公开服务器内部结构的做法,因为客户端服务器架构的主要目的是服务器的独立性.

I think that it is by design that the server errors are not propogated to client. This is in general a practice to not expose server internals to clients as the main purpose of Client Server architecture is independence of server.

您仍然可以通过使用 故障异常

用故障合同装饰您的服务声明

Decorate your service declaration with a fault contract

[ServiceContract]
public interface IServer
{
    [OperationContract]
    [FaultContract(typeof(MyApplicationFault))]
    DTO GetDTO();
}

然后在 servcie 实现中捕获错误并抛出错误异常.

Then catch errors in servcie implementation and throw a fault exception.

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class Server : IServer
    {
        public DTO GetDTO()
        {
            try
              {
                   DTO dto = new DTO();
                   dto.dto = dto;
                   return dto;
               }
            catch (Exception ex)
                 {
                     MyApplicationFault fault = new MyApplicationFault(...);
                     throw new FaultException<MyApplicationFault>(fault);
                 }
        }

    }

并在客户端捕获异常

IServer proxy = ...;    //Get proxy from somewhere
try 
{
    proxy.GetDTO();
}
catch (TimeoutException) { ... }
catch (FaultException<MyApplicationFault> myFault) {
    MyApplicationFault detail = myFault.Detail;
    //Do something with the actual fault
}
catch (FaultException otherFault) { ... }
catch (CommunicationException) { ... }

希望这会有所帮助.有关不错的教程,请参阅 故障异常代码项目教程

Hope this helps. For a nice tutorial please see Code Project Tutorial on Fault Exception

这篇关于没有异常消息详细信息的 WCF CommunicationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆