Our MVC application calls a WebAPI action using HttpClient. I decided to inject the HttpClient using StructureMap and override dispose in the controller
public HomeController(HttpClient httpClient)
{
_httpClient = httpClient;
}
protected override void Dispose(bool disposing)
{
if (disposing && _httpClient != null)
{
_httpClient.Dispose();
}
base.Dispose(disposing);
}
The StructureMap ObjectInitialize basically looks like this..
x.For<HttpClient>().Use(() => new HttpClient() { BaseAddress = "my/uri/"});
When I build this, CodeAnalysis complains "Dispose objects before losing scope"
and points to the IoC code.
Can I Suppress that, or where do I need to dispose of the HttpClient? I also tried
protected void Application_EndRequest(object sender, EventArgs e)
{
ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}
But I still get that rule violation.