Answered by:
The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

Question
-
User1279383592 posted
Hello Everyone,
I'm working on WCF. & i'm experiencing the subjected error. I searched online & found that writing a method (which is the following) could resolved this...but doing so gives me some compile time error related to GetWebRequest :
protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);
webRequest.KeepAlive = false;
webRequest.ProtocolVersion=HttpVersion.Version10;
return webRequest;
}
Please let me know, what to do to fix the subjected problem.
Thanks in Advance
Nabh19
Saturday, June 1, 2013 2:45 AM
Answers
-
User260886948 posted
Hi,
When occur the below problem, you must explicitly set the KeepAlive property to FALSE so that the connection will be closed by itself and re opened for every request. This will avoid the issue of attempting to use a closed connection.
There is no direct way you can set the KeepAlive to FALSE. This is a 'Protected' property on the WebRequest. When you use web services, you must first derive a class from your webservice class and then set this flag to FALSE in the inherited class.
Assume you have a web reference with the name 'Banking' and a webservice class called 'Transaction'. Typically, you will use the class as follows in your client:
dim obj as new Banking.Transaction()
obj.MakeTransaction()
To set the KeepAlive property, you must inherit a class from Banking.Transaction in your client. Create a new class called "BankTransaction" in the client project. Now inherit the "GetWebRequest" method in this class as shown below:
Public Class BankTransaction
Inherits Banking.Transaction
Protected Overrides Function GetWebRequest(ByVal uri As Uri) As System.Net.WebRequest
Dim webRequest As System.Net.HttpWebRequest
webRequest = CType(MyBase.GetWebRequest(uri), System.Net.HttpWebRequest)
' Setting KeepAlive to false
webRequest.KeepAlive = False
return webRequest
End Function
End Class
We are explicitly setting the property KeepAlive to FALSE in the above inherited method. Now, use the inherited class to make the webservice calls. This will solve the error.For more information, please try to refer to:
http://www.dotnetspider.com/resources/2596-e-underlying-connection-closed-A-connection.aspx .Hope it can help you.
Best Regards,
Amy Peng- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 3, 2013 10:19 PM
All replies
-
User220959680 posted
compile time error related to GetWebRequestwebRequest.KeepAlive = false;
Refer http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.keepalive.aspx
Let us know the current exception.
Sunday, June 2, 2013 4:58 PM -
User260886948 posted
Hi,
When occur the below problem, you must explicitly set the KeepAlive property to FALSE so that the connection will be closed by itself and re opened for every request. This will avoid the issue of attempting to use a closed connection.
There is no direct way you can set the KeepAlive to FALSE. This is a 'Protected' property on the WebRequest. When you use web services, you must first derive a class from your webservice class and then set this flag to FALSE in the inherited class.
Assume you have a web reference with the name 'Banking' and a webservice class called 'Transaction'. Typically, you will use the class as follows in your client:
dim obj as new Banking.Transaction()
obj.MakeTransaction()
To set the KeepAlive property, you must inherit a class from Banking.Transaction in your client. Create a new class called "BankTransaction" in the client project. Now inherit the "GetWebRequest" method in this class as shown below:
Public Class BankTransaction
Inherits Banking.Transaction
Protected Overrides Function GetWebRequest(ByVal uri As Uri) As System.Net.WebRequest
Dim webRequest As System.Net.HttpWebRequest
webRequest = CType(MyBase.GetWebRequest(uri), System.Net.HttpWebRequest)
' Setting KeepAlive to false
webRequest.KeepAlive = False
return webRequest
End Function
End Class
We are explicitly setting the property KeepAlive to FALSE in the above inherited method. Now, use the inherited class to make the webservice calls. This will solve the error.For more information, please try to refer to:
http://www.dotnetspider.com/resources/2596-e-underlying-connection-closed-A-connection.aspx .Hope it can help you.
Best Regards,
Amy Peng- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 3, 2013 10:19 PM