How does WCF ClientBase <TChannel> handle TCP connections on deletion?
Some time ago, I came across an interesting article explaining that placing HttpClientin the used block will destroy the object when the code has executed but did not close the TCP socket, and the TCP state will eventually go to TIME_WAIT and stay in this state for further activity for 4 minutes (default).
So basically using this a few times:
using(var client = new HttpClient())
{
//do something with http client
}
results in many open TCP connections located at TIME_WAIT .
Here you can read all of this:
You are using HttpClient incorrectly and destabilizing your software
So, I was wondering what would happen if I did the same with the derived class of the class ClientBase<TChannel>created by Visual Studio when you right-click the project and select the Add service link .. command and implemented this:
//SomeServiceOutThere inherits from ClientBase
using (var service = new SomeServiceOutThere())
{
var serviceRequestParameter = txtInputBox.Text;
var result = service.BaddaBingMethod(serviceRequestParameter);
//do some magic (see Fred Brooks quote)
}
However, I was not able to recreate exactly the same behavior, and I wonder why.
- I created a small desktop application and added a link to the WCF service served by IIS.
- Then I added a button that basically calls the code through a block of code, as shown above.
- When I first start the service, I start netsat for IP, and this is the result:
- So far so good. I pressed the button again and, of course, established a new connection, while the first one went into TIME_WAIT state :
- , , ESTABLISHED
HttpClient( , ).
, WCF , , .
, , , , HttpClient:
ClientBase (, . ), , , , , , (, Windows, WCF, Desktop).
?
WCF HttpClient . WCF, , HttpWebRequest, API , , , , HttpClient .
WCF , , HTTP- . , , . , HttpClient.
4.6.2 Desktop.NET Framework HttpClienthandler.Dispose:
ServicePointManager.CloseConnectionGroups(this.connectionGroupName);
CoreClr, . , . - this.connectionGroupName = RuntimeHelpers.GetHashCode(this).ToString(NumberFormatInfo.InvariantInfo); ctor. connectionGroupName . , .
, . TIME_WAIT. . ( ) , . TCP- ( ). , .
, ? , , , , TIME_WAIT.
: 100k .


