我想要一个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实现的 Contract 列表中找不到 Contract 名称“IMetadataExchange” . 将ServiceMetadataBehavior直接添加到配置文件或ServiceHost以启用对此 Contract 的支持 .
我觉得你不能使用TCP发送元数据,但这就是为什么有mexTcpBinding选项的情况?
1 Answer
好吧,如果你想拥有元数据 - TCP或HTTP - 你还需要包含
serviceMetadata
行为!当然,你不能有"HttpGetEnabled" - 但行为本身必须存在才能启用元数据交换(因此
IMetadataExchange
Contract ) .