我想让 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 选项?
好吧,如果您想要元数据 - TCP 或 HTTP - 您仍然需要包含serviceMetadata
行为!
<behaviors>
<serviceBehaviors>
<behavior name="WcfTcpTest.Service1Behavior">
<serviceDebug includeExceptionDetailInFaults="True" />
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>
当然,您不能在其上添加“HttpGetEnabled” - 但行为本身必须存在才能启用元数据交换(因此IMetadataExchange
合同)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)