简体   繁体   English

启用可靠 Session 时 WCF 合同过滤器不匹配

[英]WCF ContractFilter Mismatch when enabling Reliable Session

I have a WCF service hosted in a Windows Service.我有一个托管在 Windows 服务中的 WCF 服务。 I then have a GUI(client) that then communicates to this service.然后我有一个 GUI(客户端),然后与该服务通信。 It has recently been reported that communication with the service stops after being idle for 10 minutes.最近有报道称,与服务的通信在空闲 10 分钟后停止。

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.因此,我想增加接收超时并启用可靠会话并将 inactivityTimeout 设置为更长。 However when I do this in both the WCF service and clients app.config file I get the following error:但是,当我在 WCF 服务和客户端 app.config 文件中执行此操作时,我收到以下错误:

客户端产生的错误

Setting reliableSession enabled="False" causes the client and service to run.设置reliableSession enabled="False" 会导致客户端和服务运行。 ( although only for 10 minutes ) (虽然只有10分钟)

Doing some research the suggestion is this is because of one of the following three reasons:做一些研究,建议是因为以下三个原因之一:

  • You have different contracts between client and sender.客户和发件人之间有不同的合同。
  • You're using a different binding between client and sender.您在客户端和发件人之间使用了不同的绑定。
  • The message security settings are not consistent between client and sender.客户端和发送者之间的消息安全设置不一致。

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:这是我的服务和客户端的 app.config:

Service服务

<?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>

Client客户

<?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>

Any help would be greatly appreciated.任何帮助将不胜感激。

There are several issues in your configuration.您的配置中有几个问题。 This project seems to be a class library project.这个项目似乎是一个 class 库项目。 Please use the WCF service application template.请使用 WCF 服务申请模板。 Then the base address of the service should be configured in IIS, not in the configuration file.那么服务的基地址应该配置在IIS中,而不是配置文件中。 In addition, your binding configuration will not take effect because you don't apply it in the service endpoint.此外,您的绑定配置不会生效,因为您没有在服务端点中应用它。

<endpoint address="" binding="netTcpBinding" bindingConfiguration=""

Please refer to my example.请参考我的例子。
Server (WCF service application).服务器(WCF 服务应用程序)。

IService.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(string value);
}

Service1.svc服务1.svc

public class Service1 : IService1
    {
        public string GetData(string value)
        {
            return DateTime.Now.ToLongTimeString();
        }
}

Web.config Web.config

  <system.serviceModel>
    <services>
      <service name="WcfService3.Service1">
        <endpoint address="" binding="netTcpBinding" 
          contract="WcfService3.IService1">
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" 
          contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Then we deploy the service in the IIS.然后我们在 IIS 中部署服务。 Before we deploy it, we should enable windows features for net.tcp protocol.在我们部署它之前,我们应该为net.tcp协议启用 windows 功能。
在此处输入图像描述
Add the support for the net.tcp protocol on the website.在网站上添加对net.tcp协议的支持。
在此处输入图像描述
Then add the site binding.然后添加站点绑定。
在此处输入图像描述
One more thing we need to pay attention to is ensuring the below service is on running state.我们需要注意的另一件事是确保以下服务正在运行 state。
在此处输入图像描述
Client.客户。 (by adding service reference, the client proxy sends an invocation) (通过添加服务引用,客户端代理发送调用)

static void Main(string[] args)
{
    ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
    //by default, the nettcpbinding uses windows credential, we should provide server windows account.
    client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
    client.ClientCredentials.Windows.ClientCredential.Password = "abcd1234!";
    try
    {
        var result = client.GetData("Hello");
        Console.WriteLine(result);
    }
    catch (Exception)
    {
        throw;
    }    
}

App.config.应用程序配置。

<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="NetTcpBinding_IService1">
                <security>
                    <transport sslProtocols="None" />
                </security>
            </binding>
        </netTcpBinding>
    </bindings>
    <client>
        <endpoint address="net.tcp://vabqia969vm/Service1.svc" binding="netTcpBinding"
            bindingConfiguration="NetTcpBinding_IService1" contract="ServiceReference1.IService1"
            name="NetTcpBinding_IService1">
            <identity>
                <servicePrincipalName value="host/vabqia969VM" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

Result.结果。
在此处输入图像描述
Feel free to let me know if there is anything I can help with.如果有什么我可以帮忙的,请随时告诉我。

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.通过将客户端和服务设置为空字符串,错误被删除。

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.超时问题 - 即使启用了可靠连接并且对于不活动和接收超时的长时间超时,10 分钟的连接问题仍然存在。 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.相反,它建议处理故障异常并尝试重新连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM