我有一个独立的 WCF 服务在防火墙后面的服务器上运行。它当前使用 wsDualHttpBinding
作为服务利用回调。客户端在局域网环境下工作正常。我们已打开防火墙以允许自定义端口上的流量,因此现在可以从外部 LAN 进行服务发现。
由于 wsDualHttpBinding
的性质,如果客户端位于其自己的防火墙后面,这显然无法工作。因此,很自然地,我想到了 netTcpBinding,它应该可以解决双向问题。但奇怪的是,当服务配置 XML 更新为在相同端口上包含 netTcpBinding
(和/或排除 wsDualHttpBinding
)时,甚至服务发现不再起作用。
我想知道是否还缺少其他内容。我遵循了 How to: Use netTcpBinding with Windows Authentication and Message Security in WCF Calling from Windows Forms 中的准确配置建议和来自 Windows Communication Foundation QuickStart - Multiple Binding VS2010 .
配置:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBindingEndpointConfig">
<security mode="Message" />
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="Service1.Service1.Service">
<endpoint address="Service1" binding="wsDualHttpBinding" contract="Service1.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<endpoint address="" binding="netTcpBinding"
bindingConfiguration="NetTcpBindingEndpointConfig"
name="NetTcpBindingEndpoint" contract="Service1.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:9999/Service1.Service1/"/>
<add baseAddress="net.tcp://localhost:9999/Service1.Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
最佳答案
如果您只能使用单个端口并且必须使用 netTcpBinding,请尝试以这种方式配置您的服务:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBindingEndpointConfig">
<security mode="Message" />
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="Service1.Service1.Service">
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
<endpoint address="" binding="netTcpBinding"
bindingConfiguration="NetTcpBindingEndpointConfig"
name="NetTcpBindingEndpoint" contract="Service1.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:9999/Service1.Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="False"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
添加服务引用时尝试使用此地址:
net.tcp://localhost:9999/Service1.Service1/mex
关于防火墙后面的 wsDualHttpBinding 和 netTcpBinding 的 WCF 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10311446/