Summary of causes of recent failures of the WCF Service after the system goes online

Source: Internet
Author: User
Tags dot net

Preface

After the system has recently been released and changed to various bugs, its functions are relatively stable. Due to the recent increase in the number of users, exceptions such as logon failures and page Errors often occur, it was later discovered that the WCF Service was killed from time to time. Then I began to analyze the problem. The preliminary solution is as follows:

1. After the Web end calls the WCF Service for use, the new link cannot be accessed because it is not released or closed.

2. Increase the default number of connections. The default number of connections is small.

3. provide different instances of the same WCF Service

1. After the Web end calls the WCF Service for use, the new link cannot be accessed because it is not released or closed.

First, make sure that the connection established by the client is closed after use. Do not use traditional using statements to call WCF. Here @ Dudu has encountered this problem: http://www.cnblogs.com/dudu/archive/2011/01/18/1938144.html. The analysis is also comprehensive and will not be detailed here.

However, I feel that the better processing method may be as follows, that is, the method in @ Dudu is simply encapsulated. However, I feel that there is still room for optimization and I have not tried it yet.

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

The following method seems to be used for calling. It looks concise, but it still seems complicated. I don't know if I can handle a line of return code directly?

        public static DocAppend GetDocAppend(string dwid, string actionId)        {            DocAppend docAppend = new DocAppend();            new DocumentServiceV2.DocumentServiceV2Client().Using(channel => docAppend = channel.GetDocAppend(dwid, actionId));            return docAppend;        }

Another method is to close the link. This method is similar to the above method and can be encapsulated. The above method is used for the moment in the system.

            Document document = null;            DocumentServiceClient client = new DocumentService.DocumentServiceClient();            try            {                document= client.GetDocument(id);                if (client.State != System.ServiceModel.CommunicationState.Faulted)                {                    client.Close();                }            }            catch (Exception ex)            {                client.Abort();            }            return document;

 

2. Increase the default number of connections. The default number of connections is small.

If nettcp is used for binding, and in Windows 7, the default number of concurrent connections is 10.

This is the original configuration file

<binding name="netTcpBindConfig" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10" maxReceivedMessageSize="2147483647"> 

After the project is transplanted to the server

<binding name="netTcpBindConfig" closeTimeout="00:30:00" openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="100" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="100" maxReceivedMessageSize="2147483647"> 

However, sometimes the problem still cannot be solved, so I thought if I needed to configure the behavior, so I changed the number of connections for the behavior.

      <serviceBehaviors>        <behavior name="ThrottledBehavior">          <serviceMetadata httpGetEnabled="true" />          <serviceDebug includeExceptionDetailInFaults="true" />          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>          <serviceThrottling maxConcurrentCalls="5000" maxConcurrentSessions="5000" maxConcurrentInstances="5000" />        </behavior>      </serviceBehaviors>

Maxconcurrentcils: the maximum number of server operations that can be processed at the same time. If the number of calls exceeds, You need to insert other method calls into the queue for processing.

Maxconcurrentsessions: Maximum number of simultaneous transfers or application sessions.

Maxconcurrentinstances: Maximum number of instances.

Increase the number of connections

In the HTTP protocol, the maximum number of concurrent connections for the same HTTP request is 2. This value is too small. The current browser basically does not follow this restriction, but the system. net on the dot NET Platform still follows this standard by default. As a result, when using httpwebrequset or WebClient to access a website through multiple threadsThe connection is abnormally closed.And greatly reduce the efficiency.

The value of this restriction can be set or configured by yourself. This value is valid only for subsequent HTTP requests.

  <system.net>    <connectionManagement>      <add address="*" maxconnection="5000"/>    </connectionManagement>  </system.net>

 

3. provide different instances of the same WCF Service

3. First, check a WCF Service class.

There is an overloaded version of N multi-constructors. Let's take a look at the second constructor.

        public DocumentWriteServiceClient(string endpointConfigurationName) :                 base(endpointConfigurationName) {        }

That is, input the configuration name and code class instance. In the WCF configuration section of Web. config, perform the following processing:

      <endpoint address="http://localhost:8700/Design_Time_Addresses/SinoSZJS.WebWCF/DocumentWriteService/" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICommonBinding" contract="DocumentWriteService.IDocumentWriteService" name="1">        <identity>          <dns value="localhost" />        </identity>      </endpoint>      <endpoint address="http://localhost:8700/Design_Time_Addresses/SinoSZJS.WebWCF/DocumentWriteService/" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICommonBinding" contract="DocumentWriteService.IDocumentWriteService" name="2">        <identity>          <dns value="localhost" />        </identity>      </endpoint>      <endpoint address="http://localhost:8700/Design_Time_Addresses/SinoSZJS.WebWCF/DocumentWriteService/" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICommonBinding" contract="DocumentWriteService.IDocumentWriteService" name="3">        <identity>          <dns value="localhost" />        </identity>      </endpoint>

Modify the client call code

DocumentWriteServiceClient client = new DocumentWriteServiceClient();

Change

DocumentWriteServiceClient client = new DocumentWriteServiceClient(new Random().Next(1, 4).ToString());

That is to say, the client randomly selects one from multiple hosts on the WCF server to generate code-class instances. To put it bluntly, it splits one WCF host into three, and the client calls one of the three at random.

If we need to consider a large number of concurrent operations, the pseudo-random number may have some problems, but this should not be difficult to solve. we have to write another algorithm similar to the pseudo-random number, you only need to ensure that no repeated numbers (or characters) within the specified range are generated.

Summary

Currently, these three methods effectively prevent the re-Failure of the WCF Service. At least the service has been running stably for the past few days, and there are not many exceptions, which is very gratifying. I don't know if there are other methods for processing the WCF Service. Let the bloggers give me some advice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.