WCF ContractFilter Mismatch when enabling Reliable Session

﹥>﹥吖頭↗ 提交于 2021-01-29 07:41:51

问题


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.

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:

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:

  • 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:

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.


回答1:


There are several issues in your configuration. This project seems to be a class library project. Please use the WCF service application template. Then the base address of the service should be configured in IIS, not in the configuration file. 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).

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>

Then we deploy the service in the IIS. Before we deploy it, we should enable windows features for net.tcp protocol.

Add the support for the net.tcp protocol on the website.

Then add the site binding.

One more thing we need to pay attention to is ensuring the below service is on running 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.




回答2:


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



来源:https://stackoverflow.com/questions/58555953/wcf-contractfilter-mismatch-when-enabling-reliable-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!