繁体   English   中英

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

[英]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);
}

服务1.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协议的支持。
在此处输入图像描述
然后添加站点绑定。
在此处输入图像描述
我们需要注意的另一件事是确保以下服务正在运行 state。
在此处输入图像描述
客户。 (通过添加服务引用,客户端代理发送调用)

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;
    }    
}

应用程序配置。

<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 分钟的连接问题仍然存在。 然后我阅读了一篇文章,该文章建议长时间超时是错误的做法。 相反,它建议处理故障异常并尝试重新连接。

暂无
暂无

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

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