当抛出FaultException时WCF抛出CommunicationException

标签 wcf faultexception

解决方案:

一些跟踪显示,由于我的异常 T 未正确序列化,所以引发了 CommunicationException;因为,在两层深处,我有一个匿名类型的对象,它是不可序列化的。删除它并冒泡更改似乎可以修复它。在此之前我还做了一些小事,但我一辈子都不记得那是什么了,只是它没有在配置中完成。

我从我的痕迹中收到消息,例如:

Type 'RebuiltWCFService.Requests.HelloWorldRequest' with data contract name 'HelloWorldRequest:http://schemas.datacontract.org/2004/07/RebuiltWCFService.Requests' is not expected. 
Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.

原帖

我今天遇到了一个看似奇怪的问题,但我找不到答案!

问题:当我抛出FaultException 时,我的服务抛出CommunicationException!如果我不抛出异常,它不会执行此操作。

在我的服务中,我正确定义了故障契约:

[OperationContract]
[FaultContract(typeof(Faults.HelloWorldFault))]
Responses.HelloWorldResponse HelloWorld(Requests.HelloWorldRequest parameter);

然后在错误条件下我抛出正确类型的异常:

if (_errors.Count() > 0)
{
    Faults.HelloWorldFault fault = new Faults.HelloWorldFault(_errors);
    throw new FaultException<Faults.HelloWorldFault>(fault, new FaultReason("There are one or more errors in the request. Check the 'Errors' property for more detaisl"));
}

然后我在客户端捕获它:

try
{
    response = client.HelloWorld(new BasicService.HelloWorldRequest() { Number = 49 });
    client.Close();
    Console.WriteLine(String.Format("Response message: {0}, Response number: {1}", response.Message, response.Number));
}
catch (FaultException<BasicService.HelloWorldFault> ex)
{
    ...
}

这一切对我来说似乎都不错,而且应该可行。然而,一旦我去测试我的错误子句(通过提供错误的数据,例如缺失的字段),整个事情就在我身上消失了。当我抛出FaultException时,服务会抛出带有消息的CommunicationException

An error occurred while receiving the HTTP response to http://localhost:8732/Design_Time_Addresses/RebuiltWCFService/Service1/. 
This could be due to the service endpoint binding not using the HTTP protocol. 
This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). 
See server logs for more details.

有人可以对此提供一些见解吗?我正在使用 basicHttp 绑定(bind),并且还尝试使用 wsHttp。我将根据要求发布我的配置文件。

最佳答案

FaultException 是 CommunicationException 的子级。所以您的代码中发生的事情没有任何问题。

如果您在处理时不确定异常是什么,通常会报告为 CommunicationException。如果您想以自己的方式处理特定异常,请使用以下结构。

try
{ ... }
catch (FaultException<MyDemoException> me)
{ ... }
catch (FaultException fe)
{ ... }
catch (CommunicationException ce)
{ ... }
catch (Exception ex)
{ ... }

在上面的结构中,Exception 是 CommunicationException 的父级。 CommunicationException是FaultException等的父类。

System.Object 
  System.Exception
    System.SystemException
      System.ServiceModel.CommunicationException
        System.ServiceModel.FaultException
          System.ServiceModel.FaultException<TDetail>
            System.ServiceModel.Web.WebFaultException<T>

关于当抛出FaultException时WCF抛出CommunicationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6547466/

相关文章:

asp.net-mvc - 在 .NET 中实现基于 REST 的 Web 服务的最佳方法是什么?

c# - wcf FaultException 原因

WCF - 抛出异常或 FaultException?

Wcf异常处理抛出错误

wcf - 抛出错误代码 ("Receiver")但响应为 "Server"

wcf - 从 WCF 服务抛出异常而不会出错

javascript - ajax调用wcf服务出错

c# - 使用 WCF 和 Data Contract Serializer 时需要生成 XmlSerializers 程序集

wcf - 关闭具有线程的 Windows 服务

c# - 如何异步调用我的 WCF 服务?