是否可以使WcfTestClient适用于自定义传输通道?

时间:2009-01-27 18:56:47

标签: c# .net wcf interface wcf-binding

目标

我希望能够在我设计的托管框架内通过WCF托管和连接到vanilla套接字服务器。我希望能够使用WCF来编码今天必须由套接字程序员手动管理的传输和协议通信。这将允许我与Linux服务器守护程序的最终互操作性,除了传统的套接字和专有协议之外什么也不暴露。我只对此时通常使用WcfTestClient验证传输通道层感兴趣。我的理解是WcfTestClient不支持复杂的服务方法。

是否有人认为可以使WcfTestClient适用于自定义传输通道?能够通常使用此客户端测试任意数量的自定义传输通道真的很棒。

概述

我正在努力了解Windows SDK中包含的WCF Udp示例,该示例通常位于C:\ Program Files \ Microsoft SDKs \ Windows \ v6.1 \ Samples \ WCFSamples \ TechnologySamples \ Extensibility \ Transport \ Udp \ CS,假设WCFSamples.zip文件是从Samples目录中完全提取的。

这是我到目前为止采取的步骤:

  • (成功):在Visual Studio 2008中成功运行解决方案的服务和客户端。

  • (成功):使用通常位于C:\ Program Files \ Microsoft Visual Studio 9.0 \ Common7 \ IDE

  • 的WcfTestClient连接到MEX端点
  • (失败):使用WcfTestClient尝试从ICalculatorContract或IDatagramContract服务合同执行方法。

例如,当我执行Hello()方法时,收到以下错误:

  

友情致辞:

     
    

无法调用该服务。可能     原因:服务处于脱机状态或     交通不便;客户端     配置不匹配     代理;现有代理无效。     有关更多信息,请参阅堆栈跟踪     详情。你可以尝试恢复     启动新代理,恢复到     默认配置或刷新     服务。

  
     

错误详情:

     
    

上的CustomBinding     ServiceEndpoint与合同     'IDatagramContract'缺乏     TransportBindingElement。一切     绑定必须至少有一个绑定     源自的元素     TransportBindingElement。在     System.ServiceModel.Channels.Binding.EnsureInvariants(字符串     contractName)at     System.ServiceModel.Description.ServiceEndpoint.EnsureInvariants()     在     System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint     serviceEndpoint)at     System.ServiceModel.ChannelFactory.CreateFactory()     在     System.ServiceModel.ChannelFactory.OnOpening()     在     System.ServiceModel.Channels.CommunicationObject.Open(时间跨度     超时)at     System.ServiceModel.ChannelFactory.EnsureOpened()     在     System.ServiceModel.ChannelFactory 1.CreateChannel(EndpointAddress address, Uri via) at System.ServiceModel.ChannelFactory 1.CreateChannel()     在     System.ServiceModel.ClientBase 1.CreateChannel() at System.ServiceModel.ClientBase 1.CreateChannelInternal()     在     System.ServiceModel.ClientBase`1.get_Channel()     在DatagramContractClient.Hello()

  

了解客户端错误

UdpTransport项目中定义的UdpTransportBindingElement肯定是从TransportBindingElement派生的,如下所示,所以我认为WcfTestClient配置文件中必定缺少某些内容和/或可能我必须以某种方式提供测试客户端有更多信息。我尝试将Udp解决方案的客户端项目的System.ServiceModel部分复制到WcfTestClient的配置文件中,以及将传输程序集dll复制到与测试客户端相同的文件夹中,但是我收到了同样的错误。

我的理解是MEX端点应足以获取调用服务上的简单方法所需的信息。当然,我理解这个故事可能还有更多,考虑到我正在努力创建一个旨在测试开箱即用传输通道的客户端与自定义传输通道一起工作。

/// <summary>
/// Udp Binding Element.  
/// Used to configure and construct Udp ChannelFactories and ChannelListeners.
/// </summary>
public class UdpTransportBindingElement 
    : TransportBindingElement // to signal that we're a transport
    , IPolicyExportExtension // for policy export
    , IWsdlExportExtension

ITransportPolicyImport对自定义传输通道是否重要?

我询问ITransportPolicyImport,因为这是一个由TransportBindingElement的标准WCF派生实现的接口,但是Udp示例没有实现此接口,我在Web搜索或Safari上找不到任何有用的东西。它可能甚至不相关。

例如......

public class HttpTransportBindingElement :
    : TransportBindingElement
    , IWsdlExportExtension
    , IPolicyExportExtension
    , ITransportPolicyImport

样本配置......

服务配置如下所示:

  <system.serviceModel>

    <!-- 
         add our udpTransport handler for use by binding declarations 
     -->
    <!-- 
         add our standard binding handler for use by binding declarations 
     -->
    <extensions>
      <bindingElementExtensions>
        <add name="udpTransport" type="Microsoft.ServiceModel.Samples.UdpTransportElement, UdpTransport" />
      </bindingElementExtensions>
      <bindingExtensions>
        <add name="sampleProfileUdpBinding" type="Microsoft.ServiceModel.Samples.SampleProfileUdpBindingCollectionElement, UdpTransport" />
      </bindingExtensions>
    </extensions>

    <services>
      <service name="Microsoft.ServiceModel.Samples.ConfigurableCalculatorService" behaviorConfiguration="udpServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/udpsample"/>
          </baseAddresses>
        </host>
        <endpoint address="soap.udp://localhost:8001/" 
            binding="sampleProfileUdpBinding" 
            bindingConfiguration="CalculatorServer"
            contract="Microsoft.ServiceModel.Samples.ICalculatorContract" />
        <endpoint address="soap.udp://localhost:8002/datagram" 
            binding="customBinding" 
            bindingConfiguration="DatagramServer"
            contract="Microsoft.ServiceModel.Samples.IDatagramContract" />
        <endpoint address="mex"
            binding="mexHttpBinding"
            contract="IMetadataExchange" />
      </service>
    </services>

    <bindings>
        <!-- 
         server bindings 
          -->
        <sampleProfileUdpBinding>
            <binding name="CalculatorServer" clientBaseAddress="soap.udp://localhost:8003/" />
            <binding name="DatagramServer" reliableSessionEnabled="false" />
        </sampleProfileUdpBinding>

        <customBinding>
        <binding name="DatagramServer">
            <binaryMessageEncoding />
            <udpTransport/>
        </binding>
      </customBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="udpServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <client>
      <endpoint address="" binding="sampleProfileUdpBinding" bindingConfiguration="CalculatorServer"
        contract="Microsoft.ServiceModel.Samples.ICalculatorContract"
        name="CalculatorClient" />
    </client>    
  </system.serviceModel>

解决方案中的客户端配置如下所示:

  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="CustomBinding_IDatagramContract">
          <binaryMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
              maxSessionSize="2048">
            <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          </binaryMessageEncoding>
          <UpdTransportElementClientSide maxBufferPoolSize="524288" maxMessageSize="65536"
              multicast="false" />
        </binding>
      </customBinding>
      <sampleProfileUdpBinding>
        <binding name="SampleProfileUdpBinding_ICalculatorContract" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            orderedSession="true" reliableSessionEnabled="true" sessionInactivityTimeout="00:10:00" 
            clientBaseAddress="soap.udp://localhost:8003/" />
      </sampleProfileUdpBinding>
    </bindings>
    <client>
      <endpoint address="soap.udp://localhost:8001/" binding="sampleProfileUdpBinding"
          bindingConfiguration="SampleProfileUdpBinding_ICalculatorContract"
          contract="ICalculatorContract" name="SampleProfileUdpBinding_ICalculatorContract" />
      <endpoint address="soap.udp://localhost:8002/datagram" binding="customBinding"
          bindingConfiguration="CustomBinding_IDatagramContract" contract="IDatagramContract"
          name="CustomBinding_IDatagramContract" />
    </client>
    <extensions>
      <bindingElementExtensions>
        <add name="UpdTransportElementClientSide" type="Microsoft.ServiceModel.Samples.UdpTransportElement, UdpTransport" />
      </bindingElementExtensions>
      <!-- This was added manually because svcutil.exe does not add this extension to the file -->
      <bindingExtensions>
        <add name="sampleProfileUdpBinding" type="Microsoft.ServiceModel.Samples.SampleProfileUdpBindingCollectionElement, UdpTransport" />
      </bindingExtensions>
    </extensions>
  </system.serviceModel>

2 个答案:

答案 0 :(得分:2)

可以在WCF中创建自定义传输。虽然它非常复杂!

Roman Kiss已经构建了一个Null Transport,它允许您在同一个进程中使用和托管服务,而无需通过其中一个内置传输对数据进行编组。他的CodePlex文章可在以下网址获得:

http://www.codeproject.com/KB/WCF/NullTransportForWCF.aspx

这可能是帮助您学习如何建立自己的交通工具的良好起点。祝你好运!

答案 1 :(得分:1)

我不知道自定义传输渠道。我认为根据我的经验,可能无法使Wcf测试客户端适用于自定义传输通道。