如何使用net.pipe将WCF配置为支持FaultContracts,使主机和客户端处于同一进程中? [英] How do you configure WCF to support FaultContracts where both the host and client are in the same process using a net.pipe?

查看:74
本文介绍了如何使用net.pipe将WCF配置为支持FaultContracts,使主机和客户端处于同一进程中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用net.pipe绑定为我的服务到客户端的交互创建一个进程内单元测试.像良好的WCF服务一样,它在服务操作上使用FaultContractAttribute来将可能的错误(包装的异常)暴露给元数据.我希望通过XML (App.config)配置客户端和服务端点.但是,每当引发故障时,它只是CommunicationException管道已关闭",而不是类型为FaultI.在期待.

I'm trying to create an in-process unit test for my service to client interactions using net.pipe binding. Like a good WCF service it uses FaultContractAttribute on service operations to expose possible faults (wrapped exceptions) to metadata. I would like to have the client and service endpoints configured thru XML (App.config). However, whenever a fault is thrown, it's just a CommunicationException "pipe has closed", and not the typed Fault I was expecting.

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

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

I tried Adding IMetadataExchange endpoint for net.pipe, but that didn't work. I also tried . Which being on Vista required me to netsh the ACL for the http endpoint. That too did not work.

自定义异常类:

public class ValidationException : ApplicationException { }

这是配置的最新尝试,但它会弹出在服务实现的合同列表中找不到合同名称'IMetadataExchange'"

This is the latest attempt at a config, but it pumps out "The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service"

任何链接到有关如何完成此操作的示例或建议的信息.

Any Links to examples or recommendations for how to get this done would be appreciated.

<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派生您的错误异常,因为它是可序列化的.不是ApplicationException.

If the ValidationException class you describe above is the class you are using for faults, it may be the source of your problem. You should derive your fault exceptions from FaultException because it is Serializable. ApplicationException is not.

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

Wagner is right, you need to decorate your operation definition with a FaultContract attribute giving it the type of your contract. You should also to decorate your FaultContract with DataContract and DataMember attributes as well.

这篇关于如何使用net.pipe将WCF配置为支持FaultContracts,使主机和客户端处于同一进程中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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