Close the service in time after calling the WCF Service

Source: Internet
Author: User

Calling the WCF Service is the same as using a database connection. You must disable it in time. If it is not timely, too many connections may cause the server to crash.

Test environment: the WebServer that comes with vs2010 +. NET Framework3.5 + vs2010 (different results may be returned in different environments ).

Built-in

Test code:

 1:  protected void Page_Load(object sender, EventArgs e)
 2:  {
 3:      try
 4:      {
 5:          for (int i = 0; i < 1000; i++)
 6:          {
 7:              localhost.Service1Client wcf = new localhost.Service1Client();
 8:              wcf.GetData(1);
 9:              //wcf.Close();
10:              Response.Write(i + "<br/>");
11:          }
12:      }
13:      catch (Exception ex)
14:      {
15:          Response.Write(ex.Message);
16:      }
17:  }
18:   

Simulate the creation of 1000 connections to access WCF.

Running result:

Creating 10 webservers will crash. (Some people say that WebServer has no limit on the number of connections, which is wrong ).

 

Close the connection immediately after the connection is created (call the Close () event ):

 1:  protected void Page_Load(object sender, EventArgs e)
 2:  {
 3:      try
 4:      {
 5:          for (int i = 0; i < 1000; i++)
 6:          {
 7:              localhost.Service1Client wcf = new localhost.Service1Client();
 8:              wcf.GetData(1);
 9:              wcf.Close();
10:              Response.Write(i + "<br/>");
11:          }
12:      }
13:      catch (Exception ex)
14:      {
15:          Response.Write(ex.Message);
16:      }
17:  }
18:   

Running result:

Using vs2010 is the test result. In VS2008, creating less than 800 connections will also cause WebServer to crash.

If you use. NET Framework4.0, you can create all 1000 connections without calling Close.

In short, the best way to deal with it is to close the call connection in any environment in time to avoid service crash caused by excessive connections.

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.