Best Practice with WCF ChannelFactory and Connection Timeout

I am working on a winform application that will access a WCF service hosted as a Windows service on its own. I use ChannelFactory instead of a service reference. I managed to connect and call the WCF service. The problem is that I let the program stay idle for 20 minutes and then try to make another call. I get the following error:

"The socket connection was interrupted, which may be due to an error processing your message or a delay in the remote host or a network resource problem. The local socket timeout was" 00: 00: 59.9489970. "

I am looking for best practices for connection management. Currently, I have created a function called PrepareWCFConnection (see below) that checks the status of the channel and ChannelFactory. I call this method before making any calls to WCF services. Is there a better way to handle this?

     public bool PrepareWCFConnection()
    {
        if ((channelFactory == null) || 
            (channelFactory.State == CommunicationState.Faulted) ||
            (channelFactory.State != CommunicationState.Opened))
        {
            channelFactory = new ChannelFactory<IService1>(new NetTcpBinding(), endpointAddress);
        }


        if ((proxy == null) ||
            (((IClientChannel)proxy).State == CommunicationState.Faulted) ||
            (((IClientChannel)proxy).State != CommunicationState.Opened))
        {
            proxy = channelFactory.CreateChannel(endpointAddress);
            ((IClientChannel)proxy).Open();
        }

        return true;
    }
+5
source share
3 answers

If you want to reuse an existing channel, you need to keep the channel alive by checking the service every 9 minutes. I think the default receive timeout is 10 minutes, so the channel will be disabled if it remains free from this. Or you can use trusted sessions to save feeds.

, , , . . factory, .

+4

, , , . - ( 1, ), , . "ReceiveTimeout" , , . 10 .

"InactivityTimeout", , "". - - , - , .

, "ReliableSession", "ReceiveTimeout" , "InactivityTimeout" . ReliableSession , ILM ( ), keep-alive (ack ). keep-alive ALM ( ) "InactivityTimeout", .

, ALM ( ) "ReceiveTimeout" , .

, - , "ReceiveTimeout" , "InactivityTimeout".

, "ReceiveTimeout" , - . ReliableSession :

NetTcpBinding binding = new NetTcpBinding
        {
            ReliableSession = { Enabled = true },             
            SendTimeout = TimeSpan.FromMinutes( 1 )
        };

        binding.ReliableSession.InactivityTimeout = TimeSpan.Parse( "24.20:31:23.6470000" );

app.config :

<bindings>
    <netTcpBinding>
      <binding name="netTestTcpBinding"
               receiveTimeout="24.20:31:23.6470000">
        <reliableSession inactivityTimeout="24.20:31:23.6470000"
                         enabled="true" />
      </binding>
    </netTcpBinding>
  </bindings>
<services>
  <service>
    <endpoint address="IServiceContract"
              binding="netTcpBinding"
              bindingConfiguration="netTestTcpBinding"
              name="serviceContractTcpBinding"/>
    <host>
        <baseAddresses>
             <add baseAddress="net.tcp://localhost:12001/" />
        </baseAddresses>
     </host>
    </service>                   
</services>
+1

, , wcfC.Binding.ReceiveTimeout, , , - :

    TimeSpan timeSpan = DateTime.Now - LastCallTime;
    if (timeSpan.TotalSeconds > wcfC.Binding.ReceiveTimeout.TotalSeconds || wcfC.State != CommunicationState.Opened)
    { 
       wcfC.Abort();
       wcfC = new WCFChannel();

    }    
    LastCallTime = DateTime.Now;
0

All Articles