【发布时间】:2013-05-03 22:08:13
【问题描述】:
我们的 MVC 应用程序使用 HttpClient 调用 WebAPI 操作。我决定使用 StructureMap 注入 HttpClient 并在控制器中覆盖 dispose
public HomeController(HttpClient httpClient)
{
_httpClient = httpClient;
}
protected override void Dispose(bool disposing)
{
if (disposing && _httpClient != null)
{
_httpClient.Dispose();
}
base.Dispose(disposing);
}
StructureMap ObjectInitialize 基本上是这样的..
x.For<HttpClient>().Use(() => new HttpClient() { BaseAddress = "my/uri/"});
当我构建它时,CodeAnalysis 会抱怨 "Dispose objects before losing scope" 并指向 IoC 代码。
我可以抑制它吗,或者我需要在哪里处理 HttpClient?我也试过了
protected void Application_EndRequest(object sender, EventArgs e)
{
ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}
但我仍然违反规则。
【问题讨论】:
标签: asp.net-mvc-4 dependency-injection asp.net-web-api structuremap dotnet-httpclient