gpt4 book ai didi

c# - 是否可以使 WcfTestClient 用于自定义传输 channel ?

转载 作者:可可西里 更新时间:2023-11-01 07:58:59 26 4
gpt4 key购买 nike

目标

我希望能够在我正在设计的托管框架内通过 WCF 托管和连接到普通套接字服务器。我希望能够使用 WCF 来规范当今必须由套接字程序员手动管理的传输和协议(protocol)通信。这将允许我与 Linux 服务器守护进程实现最终的互操作性,这些守护进程只暴露传统套接字和专有协议(protocol)。此时我只对一般使用 WcfTestClient 验证传输 channel 层感兴趣。我的理解是 WcfTestClient 不支持复杂的服务方法。

有人认为可以让 WcfTestClient 为自定义传输 channel 工作吗? 如果能够通用地使用此客户端来测试任意数量的自定义传输 channel ,那将是非常棒的。

概览

我正在努力了解 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() 方法时,我收到以下错误:

Friendly message:

Failed to invoke the service. Possible causes: The service is offline or inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service.

错误详情:

The CustomBinding on the ServiceEndpoint with contract 'IDatagramContract' lacks a TransportBindingElement. Every binding must have at least one binding element that derives from TransportBindingElement. at System.ServiceModel.Channels.Binding.EnsureInvariants(String contractName) at System.ServiceModel.Description.ServiceEndpoint.EnsureInvariants() at System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint) at System.ServiceModel.ChannelFactory.CreateFactory() at System.ServiceModel.ChannelFactory.OnOpening() at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at System.ServiceModel.ChannelFactory.EnsureOpened() at System.ServiceModel.ChannelFactory1.CreateChannel(EndpointAddress
address, Uri via) at
System.ServiceModel.ChannelFactory
1.CreateChannel() at System.ServiceModel.ClientBase1.CreateChannel()
at
System.ServiceModel.ClientBase
1.CreateChannelInternal() at System.ServiceModel.ClientBase`1.get_Channel() at DatagramContractClient.Hello()

了解客户端错误

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

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

/// <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 对自定义传输 channel 重要吗?

我问有关 ITransportPolicyImport 的问题,因为它是一个由标准 WCF 派生的 TransportBindingElement 实现的接口(interface),但 Udp 示例没有实现这个接口(interface),而且我在网络搜索或 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>

最佳答案

可以在 WCF 中创建自定义传输。不过这涉及面很广!

Roman Kiss 构建了一个 Null Transport,允许您在同一进程中使用和托管服务,而无需通过内置传输之一编码数据的开销。他的 CodePlex 文章位于:

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

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

关于c# - 是否可以使 WcfTestClient 用于自定义传输 channel ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/484717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com