I am using following code where I am getting fxCop voilation CA2000: Dispose object before losing scope:
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
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).This, however, is not what is wrong with this method in terms of CA2000.
So let's look at the code in question:
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.In other words the method looks somewhat like this:
The code analysis engine now sees this code:
What happened to
temp
? It fails to detect (see my note below) that it is the same instance so it thinks you lost thetemp
instance here, and this should be disposed.OK, so how about changing the code to this:
Now I get another violation, from the same rule:
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.So here is the "fixed" version of this method:
NOTE!: Only handle the specific exceptions you know or think that
LoadConfiguration
can throw, don't handleException
.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 returnsthis
.