Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WCF在引发FaultException时引发CommunicationException 解决方案:_Wcf_Faultexception - Fatal编程技术网

WCF在引发FaultException时引发CommunicationException 解决方案:

WCF在引发FaultException时引发CommunicationException 解决方案:,wcf,faultexception,Wcf,Faultexception,一点跟踪显示抛出了CommunicationException,因为我的异常T没有正确序列化存在问题;因为有两层,我有一个匿名类型的对象,它是不可分离的。移除它,然后进行冒泡式的更改似乎可以修复它。在那之前,我还做了一些小事情,但我一辈子都记不起是什么,只是没有在配置中完成 我从我的跟踪中获得消息,例如: Type 'RebuiltWCFService.Requests.HelloWorldRequest' with data contract name 'HelloWorldRequest:h

一点跟踪显示抛出了CommunicationException,因为我的异常T没有正确序列化存在问题;因为有两层,我有一个匿名类型的对象,它是不可分离的。移除它,然后进行冒泡式的更改似乎可以修复它。在那之前,我还做了一些小事情,但我一辈子都记不起是什么,只是没有在配置中完成

我从我的跟踪中获得消息,例如:

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"));
}

有人能提供一些关于这一点的见解吗?我am使用basicHttp绑定,我还尝试了wsHttp。我将根据请求发布配置文件。

FaultException是CommunicationException的子项。因此,代码中发生的事情没有错

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

try
{ ... }
catch (FaultException<MyDemoException> me)
{ ... }
catch (FaultException fe)
{ ... }
catch (CommunicationException ce)
{ ... }
catch (Exception ex)
{ ... }
试试看
{ ... }
抓住(我)
{ ... }
捕获(故障异常fe)
{ ... }
捕获(通信异常ce)
{ ... }
捕获(例外情况除外)
{ ... }
在上述结构中,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>
System.Object
系统异常
System.SystemException
System.ServiceModel.CommunicationException
System.ServiceModel.FaultException
System.ServiceModel.FaultException
System.ServiceModel.Web.WebFaultException

谢谢各位,你们帮助我更好地理解了这个问题。我的观察:我保留了一个MyData列表,用于保存任何无法序列化的通用/动态集合,因此导致关闭连接,从而导致通信异常

当我从Fault Contract中删除列表并抛出Fault样式时,它显然给出了Fault Contract异常,而不是通信异常

希望有帮助,
HydTechie.

我遇到了这个问题,因为我在错误的位置有一个或多个断点。我用
Debug..Delete all breakpoints
Ctrl-Alt-F9
)删除了所有断点,所有通信异常都消失了,并被返回的正确消息替换


是的,超时时间是60秒,所以这不应该发生,所以这可能是Visual Studio 2012的一些奇怪的工件。

如果这不能解决问题,请发布配置。它确实在一定程度上解决了问题,谢谢。现在我已经解决了这个问题,我将用它来编辑我的原始帖子。“因为有一个问题,除了我的例外,我没有正确序列化”-完成了,谢谢。关于如何解决这个问题的更多细节如何?你有种族条件问题。
try
{ ... }
catch (FaultException<MyDemoException> me)
{ ... }
catch (FaultException fe)
{ ... }
catch (CommunicationException ce)
{ ... }
catch (Exception ex)
{ ... }
System.Object 
  System.Exception
    System.SystemException
      System.ServiceModel.CommunicationException
        System.ServiceModel.FaultException
          System.ServiceModel.FaultException<TDetail>
            System.ServiceModel.Web.WebFaultException<T>