What happen if we never dispose HttpClient, .Net C#

httpremote desktopweb-apiwindows

I'm working on a windows service. Code of windows service is very simple but it has a strange manner!
In windows service I should call a WebApi each 20 seconds and save the result in a SQL Database

I'm using autofac to creat instance of HttpClient at Program.cs

// HttpClient
builder.Register(ctx =>
{
    var httpClient = new HttpClient
    {
        BaseAddress = new Uri(StaticAssets.WebApiBaseAddress)
    };
    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    return httpClient;
});

I never dispose httpClient instance manually.
After running the windows service about 8 hours, IIS not work and we can not remote to server anymore, we tested Windows Remote Desktop Connection, VNC(we have VNC on server) …
We could remote to server with KVM and stop the windows service and every thing go back.
I think this problem maybe related to httpClient instance.
Are you already see the problem? How could you resolve it?

Best Answer

HttpClient is unfortunately Disposable but it should not have been. Your problem is probably due to windows running of connections or a connection limit for service point manager

In any case the solution is to just make the instance static. This is not obvious since the interface is disposable.