Domanda

Ho un servizio di Windows che ospita una miriade WCF NetTcp. Dal client, quando comincio a fare le chiamate al servizio tramite TCP, passano attraverso bene in un primo momento, ma poi dopo pochi minuti tutti insieme iniziano a dare l'errore di timeout WCF temuto che in realtà non ha nulla a che fare con timeout:

Questa operazione richiesta inviata a net.tcp: // myserver:. 8080 / ListingService non ha ricevuto una risposta entro il timeout configurato (00:01:00)

che ho visto da altri post su questo sito che un sacco di volte che questo ha a che fare con le dimensioni max dei messaggi, ma ho già impostare questi ai limiti senza alcun risultato.

Ecco il mio codice servizio di Windows:

public partial class Service : ServiceBase
    {
        internal static ServiceHost myHost = null;

        public Service()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = 10000;
            //create host.
            var path = ConfigurationManager.AppSettings["ServiceHostAddress"].ToString();
            myHost = new ServiceHost(typeof(ListingService));
            //add endpoint.
            myHost.AddServiceEndpoint(typeof(IListingService), GetBinding(), path);
            //add behaviors.
            AddBehaviors();
            //open host.
            myHost.Open();
        }

        private void AddBehaviors()
        {
            //service metadata behavior.
            var smb = new ServiceMetadataBehavior();
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            myHost.Description.Behaviors.Add(smb);
            //service throttling behavior.
            var behavior = new ServiceThrottlingBehavior()
            {
                MaxConcurrentCalls = 10000,
                MaxConcurrentInstances = 10000,
                MaxConcurrentSessions = 10000
            };
            myHost.Description.Behaviors.Add(behavior);
            //service debug behavior.
            var serviceDebugBehavior = myHost.Description.Behaviors.Find<ServiceDebugBehavior>();
            serviceDebugBehavior.IncludeExceptionDetailInFaults = true;
        }

        private Binding GetBinding()
        {
            var queueBinding = new NetTcpBinding(SecurityMode.None);
            queueBinding.MaxConnections = 10000;
            queueBinding.MaxBufferSize = 2048000;
            queueBinding.MaxReceivedMessageSize = 2048000;
            return queueBinding;
        }

        protected override void OnStop()
        {
            if (myHost != null)
            {
                myHost.Close();
                myHost = null; 
            }
        }
    }

Questa è la configurazione di cliente nel caso in cui non fa alcuna differenza:

<system.serviceModel>
    <bindings>

      <netTcpBinding>
        <binding name="NetTcpBinding" transferMode="Buffered" hostNameComparisonMode="StrongWildcard"
          closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00"
          maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"><!-- transactionFlow="true"-->
          <security mode="None"/>
          <reliableSession enabled="false"/>
          <readerQuotas maxArrayLength="2147483647"/>
        </binding>
      </netTcpBinding>

    </bindings>
    <client>

      <endpoint
             address="net.tcp://myserver:8080/ListingService"
             binding="netTcpBinding" bindingConfiguration="NetTcpBinding"
             contract="ListingServiceProxy.IListingService" name="NetTcpBinding" />

    </client>
  </system.serviceModel>

faccio in modo di chiudere le connessioni client, ecco il codice:

public static void Using<T>(this T client, Action<T> work)
            where T : ICommunicationObject
        {
            try
            {
                work(client);
                client.Close();
            }
            catch (CommunicationException)
            {
                client.Abort();
                throw;
            }
            catch (TimeoutException)
            {
                client.Abort();
                throw;
            }
            catch
            {
                client.Abort();
                throw;
            }
        }

new ListingServiceClient().Using(client =>
                {
                    client.SaveListing(listing);
                });
È stato utile?

Soluzione

Hanno finito per timeout perché il database è stato effettivamente timeout, non è stato un problema con WCF.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top