启用可靠会话时 WCF ContractFilter 不匹配

WCF ContractFilter Mismatch when enabling Reliable Session

我在 Windows 服务中托管了一个 WCF 服务。然后我有一个 GUI(客户端),然后与该服务通信。最近有消息称闲置10分钟后与服务的通信就停止了。

我做了一些研究,看起来该服务由于不活动而正在丢弃连接。因此,我想增加接收超时并启用可靠的会话并将 inactivityTimeout 设置得更长。但是,当我在 WCF 服务和客户端 app.config 文件中执行此操作时,出现以下错误:

设置 reliableSession enabled="False" 导致客户端和服务 运行。 (虽然只有10分钟)

做了一些研究,建议是由于以下三个原因之一:

但是据我所知,客户端和服务器之间的设置/合同是一致的。我希望这是愚蠢的事情。这是我的 app.config 服务和客户:

服务

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

如有任何帮助,我们将不胜感激。

您的配置存在几个问题。该项目似乎是一个 class 图书馆项目。请使用 WCF 服务应用程序模板。那么服务的基地址应该在IIS中配置,而不是在配置文件中。此外,您的绑定配置不会生效,因为您没有在服务端点应用它。

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

请参考我的例子
服务器(WCF 服务应用程序)。

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

Service1.svc

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

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>

然后我们在IIS中部署服务。在我们部署它之前,我们应该为 net.tcp 协议启用 windows 功能。

在网站上添加对net.tcp协议的支持。

然后添加站点绑定。

我们需要注意的另一件事是确保以下服务处于 运行 状态。

Client.(通过添加服务引用,客户端代理发送调用)

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>

结果。

如果有什么我可以帮忙的,请随时告诉我。

阅读更多内容后,我发现了我所看到的错误的原因以及连接超时问题的解决方案。

错误 - 错误是因为我设置了不同的绑定配置。通过为客户端和服务设置为空字符串,错误被删除。

超时问题 - 即使启用了可靠的连接并且长时间不活动超时和接收超时,10 分钟的连接问题仍然存在。然后我读到一篇 post,它建议长时间超时是错误的做法。相反,它建议处理错误的异常并尝试重新连接。