CA2000 code parsing warning: Dispose call on 'new ContainerControlledLifetimeManager ()'

I get a warning about code analysis on some of my unit tests:

WidgetManagerTests.cs (40): CA2000: Microsoft.Reliability: in the 'WidgetManagerTests.TestInitialize ()' method, call System.IDisposable. object 'new ContainerControlledLifetimeManager () "before all references to it exit the scope.

I use Unity and Moq, this is an offensive line:

var loggingServiceMock = new Mock<ILoggingService>();
            this.unityContainer.RegisterInstance<ILoggingService>(loggingServiceMock.Object, new ContainerControlledLifetimeManager());
+3
source share
1 answer

CA2000 , , "" . , , , , , , RegisterInstance, , , .

, , ( , , , - ):

var loggingServiceMock = new Mock<ILoggingService>();

var lifetimeManager = new ContainerControlledLifetimeManager();
try
{
    this.unityContainer.RegisterInstance<ILoggingService>(loggingServiceMock.Object, lifetimeManager);
}
catch
{
    lifetimeManager.Dispose();
    throw;
}
+8

Source: https://habr.com/ru/post/1754816/


All Articles