简体   繁体   English

“ CA2000:在失去作用域之前处理对象”,构建Unity容器

[英]“CA2000: Dispose object before losing scope” building Unity container

I am using following code where I am getting fxCop voilation CA2000: Dispose object before losing scope: 我正在使用以下代码来获取fxCop变形CA2000:在失去作用域之前处理对象:

private static IUnityContainer BuildContainer()
{
   var container = new UnityContainer().LoadConfiguration();
   return container;
}

to remove this violation I have used following code: 为了消除这种违规,我使用了以下代码:

   private static IUntyContainer BuildContainer()
    {
        using(var container = new UnityContainer())
        {
           return container.LoadConfiguration();
        }
    }

But this code start throwing exception while resolving dependencies. 但是这段代码在解决依赖关系时开始引发异常。

Can someone help me with this? 有人可以帮我弄这个吗?

This violation usually stems from a couple of code patterns though you should look at the Help page for CA2000 for more information 尽管您应该查看CA2000的“ 帮助”页面以获取更多信息,但这种冲突通常源于几种代码模式。

  • Factory methods 工厂方法
  • Method chaining 方法链
  • Missing or incorrect exception handling 缺少或不正确的异常处理

For pure factory methods that have nothing else wrong it may in some cases be enough to just rename the method. 对于没有其他错误的纯工厂方法,在某些情况下仅重命名方法就足够了。 I don't know what the prefixes the rule is looking for are but you can try Construct , Create , New , Build (the one you have). 我不知道规则要查找的前缀是什么,但是您可以尝试ConstructCreateNewBuild (您拥有的前缀)。

This, however, is not what is wrong with this method in terms of CA2000. 但是,就CA2000而言,这并不是此方法的问题。

So let's look at the code in question: 因此,让我们看一下相关代码:

var container = new UnityContainer().LoadConfiguration();

Here I am going to assume that LoadConfiguration is a method that returns the same instance as it is invoked on, for method chaining, a fluent interface. 在这里,我将假定LoadConfiguration是一种方法,该方法返回的调用实例与流式接口链接的实例相同。

In other words the method looks somewhat like this: 换句话说,该方法看起来像这样:

public class UnityContainer
{
    public UnityContainer LoadConfiguration()
    {
        // load
        return this;
    }
}

The code analysis engine now sees this code: 代码分析引擎现在可以看到以下代码:

var temp = new UnityContainer();
var container = temp.LoadConfiguration();
return container;

What happened to temp ? temp发生了什么? It fails to detect (see my note below) that it is the same instance so it thinks you lost the temp instance here, and this should be disposed. 它无法检测到(请参阅下面的注释)同一实例,因此认为您在此处丢失了该temp实例,因此应将其处置。

OK, so how about changing the code to this: 好的,那么将代码更改为此:

var container = new UnityContainer();
container.LoadConfiguration();
return container;

Now I get another violation, from the same rule: 现在,根据相同的规则,我又遇到了另一个违规:

CA2000 : In method 'Program.BuildContainer()', object 'container' is not disposed along all exception paths. CA2000 :在方法“ Program.BuildContainer()”中,未沿所有异常路径放置对象“容器”。 Call System.IDisposable.Dispose on object 'container' before all references to it are out of scope. 在对对象“容器”的所有引用超出范围之前,请调用System.IDisposable.Dispose。 ConsoleApplication31 C:\\Dev\\VS.NET\\ConsoleApplication31\\ConsoleApplication31\\Program.cs 18 Active ConsoleApplication31 C:\\ Dev \\ VS.NET \\ ConsoleApplication31 \\ ConsoleApplication31 \\ Program.cs 18有效

Basically the code analysis engine now wonders what happens if LoadConfiguration throws an exception, then you would leak the temporary container object that was never returned. 基本上,代码分析引擎现在想知道,如果LoadConfiguration引发异常,那么您将泄漏从未返回的临时容器对象。

So here is the "fixed" version of this method: 因此,这是此方法的“固定”版本:

var container = new Container();
try
{
    container.LoadConfiguration();
    return container;
}
catch (Exception) // NOTE!
{
    container.Dispose();
    throw;
}

NOTE! 注意! : Only handle the specific exceptions you know or think that LoadConfiguration can throw, don't handle Exception . :仅处理您知道或认为LoadConfiguration可以抛出的特定异常,而不处理Exception

Note : When I say "fail to detect" above it doesn't mean that the code analysis engine has code in place to detect this that either fails or has a bug, it can also mean that the rule simply doesn't do that deep of an analysis, such as looking at the LoadConfiguration method and determining that it always returns this . 注意 :当我在上面说“无法检测”时,并不意味着代码分析引擎已在适当的位置检测到了失败或存在错误的代码,这还意味着规则根本没有做那么深入的工作。分析,例如查看LoadConfiguration方法并确定它始终返回this

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#CA2000在丢失范围之前处置对象 - C# CA2000 Dispose Object Before Losing Scope 控制器上的“ CA2000在失去作用域之前先放置对象” - “CA2000 Dispose objects before losing scope” on controller “ CA2000丢失范围之前处置对象”原因不明 - “CA2000 Dispose objects before losing scope ” for unknown reason CA2000在失去DirectoryEntry对象的MVC项目中的作用域之前,先处理对象 - CA2000 Dispose objects before losing scope in MVC project for DirectoryEntry object FxCop 10.0找不到CA2000“在丢失范围之前处置对象” - FxCop 10.0 can't find CA2000 “Dispose objects before losing scope” C#CA2000:使用FileStream / XmlTextReader在丢失范围之前处置对象 - C# CA2000:Dispose objects before losing scope using FileStream/XmlTextReader 我是否需要在这两个XmlDataSource对象上都调用.Dispose()? “ CA2000在失去作用域之前先处理对象”告诉我.Dispose() - Do I need to call .Dispose() on both of these XmlDataSource objects? “CA2000 Dispose objects before losing scope” tells me to .Dispose() it CA2000:处理对象警告 - CA2000: Dispose object warning CA2000:Microsoft.Reliability:在对所有引用超出范围之前调用System.IDisposable.Dispose对象'dt' - CA2000 : Microsoft.Reliability : Call System.IDisposable.Dispose on object 'dt' before all references to it are out of scope CA2000:Microsoft.Reliability在对象的所有引用超出范围之前,先对对象调用System.IDisposable.Dispose。 - CA2000 : Microsoft.Reliability call System.IDisposable.Dispose on object before all references to it are out of scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM