我想让 WCF-over-TCP 服务正常工作。我在修改自己的项目时遇到了一些问题,所以我想从 VS2008 中包含的“基本”WCF 模板开始。
这是初始的 WCF App.config,当我运行该服务时,WCF 测试客户端可以正常使用它:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfTcpTest.Service1" behaviorConfiguration="WcfTcpTest.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/WcfTcpTest/Service1/" />
</baseAddresses>
</host>
<endpoint address="" binding="wsHttpBinding" contract="WcfTcpTest.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfTcpTest.Service1Behavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
这完美地工作,完全没有问题。
我认为将其从 HTTP 更改为 TCP 将是微不足道的:将绑定更改为其 TCP 等效项并删除 httpGetEnabled serviceMetadata 元素:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfTcpTest.Service1" behaviorConfiguration="WcfTcpTest.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:1337/Service1/" />
</baseAddresses>
</host>
<endpoint address="" binding="netTcpBinding" contract="WcfTcpTest.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfTcpTest.Service1Behavior">
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
但是当我运行它时,我在 WCF 服务主机中收到此错误:
System.InvalidOperationException:在服务 Service1 实施的合同列表中找不到合同名称“IMetadataExchange”。将 ServiceMetadataBehavior 添加到配置文件或直接添加到 ServiceHost 以启用对此协定的支持。
我感觉您无法使用 TCP 发送元数据,但这就是为什么有 mexTcpBinding 选项的原因?