我的CA2000分析器(“失去作用域之前处置对象”告诉我在完成IOwin的HttpConfiguration
的自定义后处置IAppBuilder
实例。
我试图做到这一点,并且一切正常。 MSDN并没有告诉我应该处置还是应该处置HttpConfiguration
的任何信息,就像它讲述的Tasks一样好(“不要烦扰布置Task实例”)。
由于我不想让某个实例处于混乱状态,就好像我不想找到某天我的Web服务器崩溃一样,我该怎么办?
编辑,我的代码的一部分:
public void Configuration(IAppBuilder appBuilder)
{
EnableCookieAuth(appBuilder);
UseWebApi(appBuilder);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "Handled by ")]
private static void UseWebApi(IAppBuilder appBuilder)
{
appBuilder.Use<ControlMiddleware>();
using HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
appBuilder.UseNinjectWebApi(config);
config.EnsureInitialized();
}
有意义的在owin app中放置httpConfiguration
的地方,在OnAppDisposing
中
HttpConfiguration httpConfiguration = new HttpConfiguration();
app.UseWebApi(httpConfiguration);
AppProperties properties = new AppProperties(app.Properties);
CancellationToken token = properties.OnAppDisposing;
if (token != CancellationToken.None)
{
token.Register(() =>
{
httpConfiguration.Dispose();
});
}