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.