Reqnroll.Microsoft.Extensions.DependencyInjection: Root service provider not disposed
Reqnroll Version
2.3.0
Which test runner are you using?
MSTest
Test Runner Version Number
3.4.3
.NET Implementation
.NET 8.0
Test Execution Method
Visual Studio Test Explorer
Content of reqnroll.json configuration file
Not sure where to find it, but this is what I found when I searched for the file name on the Windows system drive.
{
"$schema": "https://schemas.reqnroll.net/reqnroll-config-latest.json",
"bindingAssemblies": [
]
}
Issue Description
Singleton services are not disposed when using Reqnroll.Microsoft.Extensions.DependencyInjection.ScenarioDependenciesAttribute to add servicess to the Microsoft.Extensions.DependencyInjection.ServiceCollection.
Steps to Reproduce
- Register a disposable singleton service in the service collection, using
ScenarioDependenciesAttribute, ie[ScenarioDependencies] public static IServiceCollection RegisterMyServices(). - Have the service injected into some class used by a step (resolved from the service provider)
- When the test run ends, notice that the
Disposemethod of the disposable singleton service is not triggered.
Link to Repro Project
https://github.com/olaviedoc/ReqnrollDisposeBugRepro
Update: added a repro project link.
Please see MySingletonService for the service that should be disposed (but never is).
Here's the output of the debug window when running it (abbreviated for brevity and privacy reasons):
'testhost.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.13\System.Threading.Tasks.Extensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
16:59:29:929 MySingletonService.MySingletonService executing <<<<<<<<<<<<<<<<<<<<<<
16:59:29:929 MySingletonService.UseTheService executing <<<<<<<<<<<<<<<<<<<<<<
16:59:30:001 The thread '.NET Long Running Task' (8584) has exited with code 0 (0x0).
16:59:30:001 The program '[36444] testhost.exe' has exited with code 0 (0x0).
As you can see MySingletonService.Dispose executing <<<<<<<<<<<<<<<<<<<<<< is never outputted, nor is the debugger triggering any breakpoints for the Dispose method.
As far as I known this is tricky as this will also dispose other services like the FeatureContext (but correct me if wrong).
But good to know, why do we need to dispose the instance? Is it an issue when the test is finisched?
@304NotModified The dispose behavior of MSDI is pretty strange but this is now addressed with the new design model. However, we can still do a quick fix here. And yes, it will dispose things like FeatureContext (that is normally bad), but fortunately at the current moment the dispose method of FeatureContext and ScenarioContext does not do anything, so it is not causing a direct issue.