If your ASP.NET application calls a web service (you have a reference in your App_WebReferences folder), you might come across this fairly typical System.Net.WebException. I have seen two different breeds of this exception, and the solutions for each of them are quite similiar. I will start off with the most common of the two.
The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
This is the one you are most likely to see, and the fix for this is simple and generally well-known. You need to create a class that inherits your web service class. Then you simply override the GetWebRequest function and modify the KeepAlive HTTP header accordingly…
Public Class MyWebService Inherits ServiceName.WebService Protected Overrides Function GetWebRequest(ByVal uri As Uri) As System.Net.WebRequest Dim webRequest As System.Net.HttpWebRequest = MyBase.GetWebRequest(uri) webRequest.KeepAlive = False Return webRequest End Function End Class
Simple as that. When you need to invoke the web service elsewhere in your code, just instatiate that class instead of what you would do normally…
Dim Response as String Using MyService as New MyWebService() Response = MyService.METHOD_NAME("MethodInput") End Using
And that takes care of that exception.
The underlying connection was closed: The connection was closed unexpectedly.
Sometimes, using the solution above will cause this exception. I believe that it has something to do with the way Windows Authentication is configured in IIS. I ran across a situation where I had to deploy my applicaton (with web service references) to a remote server that was not in our control; it was being hosted for us. Apparently, they were using NTLM Authentication, which requires connections to be kept-alive, hence this exception. However, using KeepAlive makes your requests totally inconsistent, in most cases. They will work one time, but will fail the next. The host was not willing to change for this for our application, obviously, so I was stuck. Finally, I was able to determine that the ConnectionGroupName property is what is used to re-establish the connection that was supposed to be "kept alive". So, you can just set that property to a random value (GUID), in the same way that I set the KeepAlive property above…
Public Class MyWebService Inherits ServiceName.WebService Protected Overrides Function GetWebRequest(ByVal uri As Uri) As System.Net.WebRequest Dim webRequest As System.Net.HttpWebRequest = MyBase.GetWebRequest(uri) webRequest.ConnectionGroupName = Guid.NewGuid().ToString() Return webRequest End Function End Class
Problem solved; that worked fantastic! I hope this has helped you out, and I also hope that I have had my final bout with the System.Net.WebException.
But I guess that you're not able to program correctly for multiple browsers.
Guess What?
There are a few basic guidelines you should be aware of before leaving a comment…
- If you choose to display your email address, it will not be detected by spam bots
- Comments are limited to 3,000 characters; so far you have used none of them
- HTML will be encoded; links and line breaks will be converted automatically
- Comments containing five or more links will be subject to moderation
