About the connection timeout of wcf, the connection timeout of wcf

Source: Internet
Author: User

About the connection timeout of wcf, the connection timeout of wcf

Today, the system on the Internet suddenly reports an error. After troubleshooting, the error message is as follows:

System. timeoutException: The request channel timed out while waiting for a reply after 00:00:00. increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. the time allotted to this operation may have been a portion of a longer timeout. ---> System. timeoutException: The request operation did not complete within the allotted timeout of 00:01:00. the time allotted to this operation may have been a portion of a longer timeout.
At System. ServiceModel. Channels. ReliableRequestSessionChannel. SyncRequest. WaitForReply (TimeSpan timeout)
At System. ServiceModel. Channels. RequestChannel. Request (Message message, TimeSpan timeout)
--- End of inner exception stack trace ---

This post is very valuable because I have searched for documents for half a day on the Internet.

I also had this timeout problem. the client requests to my WCF service wowould work 3 to 6 times, then it wowould fail with a timeout. I was passing tiny messages back and forth, so I knew "I cannot accomplish such big error with such tiny messages ";-)

The problem was I was forgetting to call. Close ()On the WCF client object! Once I added the appropriate ". Close ()", the error disappeared.

So once you're done calling the WCF service, make sure to call Close (), probably in your Finally block.


After investigation, the relevant department actually called the service and did not add. Close (). Caused.

Before the code is modified for this department, if you need to modify the server configuration, you can increase the number of service configuration connections.

<ServiceBehaviors>

<Behavior name = "ServiceBehavior">

<! -- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->

<ServiceMetadata httpGetEnabled = "true"/>

<DataContractSerializer maxItemsInObjectGraph = "10000000"/>

<! -- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->

<ServiceDebug includeExceptionDetailInFaults = "true"/>

<ServiceThrottling maxconcurrentcils = "200" maxConcurrentSessions = "100" maxConcurrentInstances = "100"/>

</Behavior>

</ServiceBehaviors>


If the wcf is hosted on iis, you can add the global configuration of iis.

Http://www.cnblogs.com/z2002m/archive/2012/10/26/1918342.html

This article is very valuable.

Verify the MaxConnection of IIS

Recently, a dear friend of the company wrote a software program to monitor IIS, which mentioned a number of connections. This number of connections is mainly used to collect WMI information,

 

Write the Code:

 

// Obtain all IIS sites

System. Management. ManagementClass mc = new System. Management. ManagementClass ("Win32_PerfFormattedData_W3SVC_WebService ");

 

// Each site

Foreach (System. Management. ManagementObject obj in mc. GetInstances ())

{

// The current number of connections for each site

Convert. ToInt64 (obj. Properties ["CurrentConnections"]. Value );//

}

 

At first glance, What Is CurrentConnections? I have been wondering how many connection requests can be received by an IIS Site at the same time by default.Maxconnection.

 

First, let's take a look at how Microsoft explains:

 

The maxconnection parameter determines how many connections can be made to a specific IP address.

This maxconnection parameter is used to determine the number of connections that each IP Address can accommodate (translation is not necessarily accurate ).

<ConnectionManagement>

<Add address = "*" maxconnection = "2"> // two

</ConnectionManagement>


Will this maxconnection be the number of concurrent connections?

 

Let's look at the http://www.microsofttranslator.com/BV.aspx? Ref = CSSKB & from = en & to = zh-chs & a = http://support.microsoft.com/kb/821268/en-us? Fr = 1

 

There is a paragraph in it that is quite clear,

 

  • MaxWorkerThreadsParameters andMaxIoThreadsSet the parameter value100.
  • MaxconnectionSet the parameter value to 12 *N(Where,NIs the number of your cpu ).
  • SetMinFreeThreadsParameter Value88 *NAndMinLocalRequestFreeThreadsParameter,76 *N.
  • MinWorkerThreadsTo50. RememberMinWorkerThreadsIt is not in the default configuration file. You must add it.
Some of these suggestions involve a simple formula involving the number of CPUs on the server. NVariable indicating the number of CPUs in the formula. If you have enabled these settings, you must use the number of logical CPUs instead of the number of physical CPUs. For example, if hyper-threading is enabled on a server with four processors NThe value in the formula will be 8Instead 4.

Note:When using this configuration, You can execute a maximum of 12 ASP. NET requests per CPU at the same time because100-88 = 12. Therefore,At least 88 *NWorker threads and 88 *NOther functions (such as Web Service callback) available for all port threads are completed ).

 

Basically, I understand. Maxconnection = 12 * Number of CPUs. Our company's servers use dual-core, that is, by default,

 

The default maximum number of connections for IIS sites on our company's servers is 24,

 

Next, let's verify that the purpose of the test is to prove whether the IIS Site can accept other connections when more than 24 access connections exist,

 

Implementation preparation:

1. Write A WEBService, which includes Methods A and B. In method A, there is Thread. Sleep (10 minutes), while Method B directly returns "OK ";

2 VS2008 load testing (simulate 24 users simultaneously accessing the webServiceA method on A server, because method A will sleep the thread for 10 minutes)

3. Collect WMI information on the server to observe the current number of WEBService connections. Use obj. Properties ["CurrentConnections"]. Value (for specific collection methods, see the beginning of this article)

 

 

This is a load test,

 

This is the test method of the Load Test Call.

 

OK. You are ready to eat .. Oh, no. Start the test. Here I will not post the diagram of the test at that time. I will tell you the test result,

 

When the load test starts, the Value of obj. Properties ["CurrentConnections"]. Value of Webservicer is increasing all the time. When it is increased to 24, it is the 24 users we have simulated,

 

We simulate writing a piece of code to access the WebServiceB method on the local machine. Note that method B does not sleep the thread, and directly Return "OK". The result waits until the timeout and no OK is displayed,

 

However, the Value of obj. Properties ["CurrentConnections"]. Value is 25,

 

If we set the number of concurrent connections to 23, then start the load test. When the number of connections reaches 23, we call Method B. The result shows that the method B is successfully called in about three seconds, show "OK ",

 

 

By the way, the autoConfig attribute is introduced in ASP. NET 2.0:

1 <processModel autoConfig="true"/>

When the value is true, autoConfig modifies the configuration during runtime as follows:

 

  • Set maxWorkerThreaders and maxIoThreads to 100
  • Set maxconnection to 12 * Number of CPUs
  • Set minFreeThreads to 88 * CPU usage
  • Set minWorkerThreads to 50

 

 

 

 

 

If you say that I have a brute-force machine and configure Niu B, these default settings are not enough. I need to use the configuration myself. OK, you can directly modify it in machine. config,

 

The following is the sample code,

<System.net>
<ConnectionManagement>
<Add address = "*" maxconnection = "24"/>
</ConnectionManagement>
</System.net>
<System. web>
<HttpRuntime minFreeThreads = "176" minLocalRequestFreeThreads = "152"/>
<ProcessModel autoConfig = "false" maxWorkerThreads = "100" maxIoThreads = "100"/>

 

Some may ask: Do I have to modify this stuff in machine. config? Can it be in web. config? Currently, I am using Web. config IN THE Webserive program.

<Add address = "*" maxconnection = "30"/> was modified. However, when the server Load balancer is used for testing, it still does not work, but it can only reach 24. This means that 30 does not work.

I know why not? According to Microsoft's instructions, it should work in web. config,

 

Note: If the number of maxconnections is increased, the number of maxWorkerThreads needs to be increased. For example, on a dual-core CPU, modify maxconnection = 25,

 

MaxWorkerThreads also needs to be modified to 101, because maxconnection = maxWorkerThreads -- minFreeThreads

 

 

 

 

Another question: If IIS crashes and an HTTP request comes in, what will IIS do? The answer should include Microsoft's official explanation.

 

 

 

 

Conclusion: The default number of concurrent connections for IIS sites is 12 * CPU, which means that the maximum number of requests that can be processed by IIS at the same time is 12 * CPU by default.

 

Welcome to shoot bricks. If you have any good suggestions, please try again. Thank you.


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.