如何配置WCF以支持FaultContracts,其中主机和客户端使用net.pipe在同一进程中?

我正在尝试使用net.pipe绑定为我的客户端交互服务创建进程内单元测试.与良好的WCF服务一样,它在服务操作上使用FaultContractAttribute来向元数据公开可能的错误(包装异常).我想通过
XML(App.config)配置客户端和服务端点.但是,每当抛出一个错误时,它只是一个CommunicationException“管道已经关闭”,而不是我所期望的类型错误.

System.ServiceModel.CommunicationException: There was an error reading from the pipe: The pipe has been ended. (109, 0x6d). 

我尝试为net.pipe添加IMetadataExchange端点,但这不起作用.我也试过了.哪个在Vista上需要我为http端点netsh的ACL.那也行不通.

自定义异常类:

public class ValidationException : ApplicationException { }

这是配置的最新尝试,但是它提出“在服务实现的合同列表中找不到合同名称’IMetadataExchange’”

任何有关如何完成此操作的示例或建议的链接将不胜感激.

<system.serviceModel>

  <client>
    <endpoint name="Client"
              contract="IService"
              address="net.pipe://localhost/ServiceTest/"
              binding="netNamedPipeBinding"
              bindingConfiguration="netPipeBindingConfig" />
  </client>

  <services>
    <service
      name="Service"
      behaviorConfiguration="ServiceFaults">
      <host>
        <baseAddresses>
          <add baseAddress="net.pipe://localhost/ServiceTest/"/>
          <add baseAddress="http://localhost/ServiceTest/"/>
        </baseAddresses>
      </host>
      <endpoint
        address=""
        binding="netNamedPipeBinding"
        bindingConfiguration="netPipeBindingConfig"

        name="ServicePipe"
        contract="IService" />
      <endpoint
        address="MEX"
        binding="mexNamedPipeBinding"
        bindingConfiguration="mexNetPipeBindingConfig"
        name="MexUserServicePipe"
        contract="IMetadataExchange" />
    </service>
  </services>

  <bindings>
    <netNamedPipeBinding>
      <binding name="netPipeBindingConfig"
               closeTimeout="00:30:00"
               sendTimeout="00:30:00" />
    </netNamedPipeBinding>
    <mexNamedPipeBinding>
      <binding name="mexNetPipeBindingConfig"></binding>
    </mexNamedPipeBinding>
  </bindings>

  <behaviors>
    <serviceBehaviors>
      <behavior name="ServiceFaults">
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
      <behavior name="MEX">
        <serviceMetadata 
          httpGetEnabled="true"
          httpGetUrl="http://localhost/ServiceTest/MEX"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>

</system.serviceModel>

最佳答案 如果您在上面描述的ValidationException类是您用于出错的类,则可能是您的问题的根源.您应该从FaultException派生故障异常,因为它是Serializable. ApplicationException不是.

Wagner是对的,您需要使用FaultContract属性来装饰您的操作定义,并为其指定合同类型.您还应该使用DataContract和DataMember属性来装饰FaultContract.

点赞