Webservice calls hangs and gets a timeout exception
Recently I encountered a problem using webservices in .Net.
At irregular intervals the code suddenly threw the following exception when calling a webservice:
System.Net.WebException: The operation has timed out
There was nothing magical in the code around the calls to the webservice where it was handled in the usual fashion:
WebserviceClass ws = new WebserviceClass();
When the code reached ws.Foo() it just waited there for over a minute before throwing the previously mentioned exception.
Since using webservices is extremely streamlined and standardized in Visual Studio I initially thought the problem had to lie somewhere else than my application, like maybe there were networking issues or the ISS server that hosted the webservice had some problems.
When troubleshooting all of the above and nothing panned out I was back to my application while scratching a hole in my head.
It turns out that when using webservices in .Net the framework can only have a maximum of two open connections to the server. My application fires off more than two requests, in addition to some scheduling tasks that run in the background of the application that also requests data from the webservice.
What was actually happening was some form of deadlock where two connections had been opened and not being closed fast enough so that my application was timing out because it just couldn’t make a connection.
Thanks to Microsoft support we found the solution to be to allow more than two open connections. This can easily be done in the app.config file of the application by adding the following:
<add address = "*" maxconnection = "10" />
Here the maximum number of connections has been increased to 10. For my application I increased it to 12, and we have never again experienced this timeout exception.
Microsoft’s documentation of the app.config setting