启用可靠会话时 WCF ContractFilter 不匹配 [英] WCF ContractFilter Mismatch when enabling Reliable Session
问题描述
我在 Windows 服务中托管了一个 WCF 服务.然后我有一个 GUI(客户端),然后与该服务进行通信.最近有消息称,与该服务的通信在空闲 10 分钟后停止.
I have a WCF service hosted in a Windows Service. I then have a GUI(client) that then communicates to this service. It has recently been reported that communication with the service stops after being idle for 10 minutes.
我做了一些研究,看起来该服务由于不活动而丢弃了连接.因此,我想增加接收超时并启用可靠会话并将 inactivityTimeout 设置得更长.但是,当我在 WCF 服务和客户端 app.config 文件中执行此操作时,出现以下错误:
I have done a bit of research and it looks like the service is discarding the connection due to inactivity. Therefore I want to increase the receive timeout and enable reliable sessions and set an inactivityTimeout to be longer. However when I do this in both the WCF service and clients app.config file I get the following error:
设置reliableSession enabled="False" 会导致客户端和服务运行.(虽然只有10分钟)
Setting reliableSession enabled="False" causes the client and service to run. ( although only for 10 minutes )
做一些研究,建议是由于以下三个原因之一:
Doing some research the suggestion is this is because of one of the following three reasons:
- 您在客户和发件人之间签订了不同的合同.
- 您在客户端和发件人之间使用了不同的绑定.
- 客户端和发件人之间的消息安全设置不一致.
但是据我所知,客户端和服务器之间的设置/合同是一致的.我希望这是愚蠢的事情.这是我的服务和客户端的 app.config:
However as far as I can tell the settings / contracts between the client and server are consistent. I'm hoping it's something stupid. Here are my app.config for service and client:
服务
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IHwResourceManagerWcfService" receiveTimeout="infinite" >
<reliableSession enabled="True" inactivityTimeout="infinite"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8523/HwResourceManagerWcfService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IHwResourceManagerWcfService"
contract="WindowsServices.IHwResourceManagerWcfService" name="NetTcpBinding_IHwResourceManagerWcfService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
<services>
<service name="MT.Tools.HwResourceManager.WCF.HwResourceManagerWcfService">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="MT.Tools.HwResourceManager.WCF.IHwResourceManagerWcfService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/HwResourceManagerWcfService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
客户
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IHwResourceManagerWcfService" receiveTimeout="infinite" >
<reliableSession enabled="True" inactivityTimeout="infinite"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8523/HwResourceManagerWcfService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IHwResourceManagerWcfService"
contract="WindowsServices.IHwResourceManagerWcfService" name="NetTcpBinding_IHwResourceManagerWcfService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
任何帮助将不胜感激.
推荐答案
在阅读更多内容后,我找到了我所看到的错误的原因和连接超时问题的解决方案.
After some more reading on this I found both the reason for the error I was seeing and a solution to the connection timeout issue.
错误 - 错误是因为我设置了不同的绑定配置.通过为客户端和服务设置一个空字符串,错误被消除.
The Error - The error was because I had different binding configurations set. By setting to an empty string for both the client and service the error was removed.
超时问题 - 即使启用了可靠连接并且不活动和接收超时的长时间超时,10 分钟的连接问题仍然存在.然后我读了一篇文章,建议长时间超时是错误的做法.相反,它建议处理出错的异常并尝试重新连接.
The timeout issue - Even with reliable connections enabled and a long timeout for both the inactivity and receive timeouts the 10 minute connection issue remained. I then read a post that suggested that doing a long timeout was the wrong thing to do. Instead it recommended handling the faulted exception and trying to-reconnect.
这篇关于启用可靠会话时 WCF ContractFilter 不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!