在我的C#WebAPI代码中,我正在创建UnityDependencyResolver
。代码分析告诉我在丢失范围时应该丢弃对象,但实际上该对象已分配给configuration.DependencyResolver
。
这里是代码(现在有一个警告抑制“解决”问题)
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "See REV-649")]
private static void WebApiSetup(IAppBuilder app)
{
using (var configuration = new HttpConfiguration())
{
// start unity
var container = UnityConfig.Container;
// var resolver = new UnityHierarchicalDependencyResolver(container);
var resolver = new UnityDependencyResolver(container);
configuration.Services.Replace(typeof(IExceptionLogger), new AllExceptionsLogger());
configuration.Services.Replace(typeof(IExceptionHandler), new UnhandledExceptionHandler());
// TODO - remove this for production code (DS)
configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
// Remove the XML formatter in particular. After this configuration the only
// available formatter is JSON, this means that whatever the accept header
// JSON will be returned.
configuration.Formatters.Clear();
configuration.Formatters.Add(new JsonMediaTypeFormatter());
var jsonSerializerSettings = configuration.Formatters.JsonFormatter.SerializerSettings;
// timestamps format
jsonSerializerSettings.DateFormatString = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffzzz";
// json lowerCamelCase on
jsonSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
// Handle enums as strings
jsonSerializerSettings.Converters.Add(new StringEnumConverter());
configuration.DependencyResolver = resolver;
configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
// Todo: Add this back in to enable authorisation
// configuration.Filters.Add(new AuthorizeAttribute());
app.UseWebApi(configuration);
}
}
此外,Klocwork表示存在无效的演员阵容。
我应该遵循提示,还是对代码分析的监督?