We have an old system with a lot of code similar below:
using(var httpClient = new HttpClient()){...}
We aren't able to update the code at the moment because that will require some approval from our boss and our client.
Recently we added some feature and we are using Unity for DI and we added the HttpClient as singleton in our DI Container
container.RegisterSingleton<HttpClient>();
and we are injecting the instance in our service. But after we deploy the changes we are getting below errors:
- One or more errors occurred. A task was canceled.
- SocketException with the message of A connection that was expected to be kept alive was closed by the server. ....
After reading some questions online, I found this HttpClient - A task was cancelled?
Disposing the HttpClient instance can cause following HTTP requests started by other instances of HttpClient to be cancelled!
Does it mean, the using() statements
was disposing the instance in DI container?