Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Scoped container dispose issue #100

Closed
BlackGad opened this issue Dec 30, 2022 · 8 comments
Closed

Scoped container dispose issue #100

BlackGad opened this issue Dec 30, 2022 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@BlackGad
Copy link

Hi!

Faced with an issue when scenario and feature containers were disposed too early in execution context.

You are handling RuntimePluginTestExecutionLifecycleEvents.AfterScenario and RuntimePluginTestExecutionLifecycleEvents.AfterFeature events to dispose scoped container. There are additional functionality after that events still in scope of scenario and feature.

Is it possible to postpone scoped DI dispose operations?

@BlackGad
Copy link
Author

Basically you need link IServiceCollection lifetime with BoDi container lifetime.

Register relay disposers directly into container from arguments with dispose: true on RuntimePluginEvents.CustomizeScenarioDependencies and RuntimePluginEvents.CustomizeFeatureDependencies events.

Example:

internal class SpecFlowRuntimePlugin : IRuntimePlugin
{
    public void Initialize(RuntimePluginEvents runtimePluginEvents, RuntimePluginParameters runtimePluginParameters, UnitTestProviderConfiguration unitTestProviderConfiguration)
    {
        runtimePluginEvents.CustomizeFeatureDependencies += RuntimePluginEventsOnCustomizeFeatureDependencies;
        runtimePluginEvents.CustomizeScenarioDependencies += RuntimePluginEventsOnCustomizeScenarioDependencies;
    }

    private void RuntimePluginEventsOnCustomizeFeatureDependencies(object sender, CustomizeFeatureDependenciesEventArgs e)
    {
        // Initialize child DI scope for feature

        var disposer = new RelayDispose(() =>
        {
            // Dispose child DI scope here
        });

        e.ObjectContainer.RegisterInstanceAs(disposer, dispose: true);
    }

    private void RuntimePluginEventsOnCustomizeScenarioDependencies(object sender, CustomizeScenarioDependenciesEventArgs e)
    {
        // Initialize child DI scope for scenario

        var disposer = new RelayDispose(() =>
        {
            // Dispose child DI scope here
        });

        e.ObjectContainer.RegisterInstanceAs(disposer, dispose: true);
    }
}
public class RelayDispose : IDisposable
    {
        private readonly Action _disposeAction;

        public RelayDispose(Action disposeAction)
        {
            _disposeAction = disposeAction;
        }

        public void Dispose()
        {
            _disposeAction?.Invoke();
        }
    }

I will create PR if you are still supporting this package. Please, let me know.

@mbhoek
Copy link
Member

mbhoek commented Dec 31, 2022

Hey @BlackGad, thanks for reporting this issue. These changes were introduced in #74 and in hindsight I should've had a better understanding of it before merging. For the next major release (aiming for right after they release SpecFlow v4) I'll be reworking a lot of these changes, and I'm also going to bring it more in line with how the official Autofac plugin works (e.g. they introduced a a [FeatureDependencies] tag for scoping to features).

Having said that I will consider your solution because it makes a lot of sense, so I'm adding it to the milestone. No need for a PR because the code will probably change a lot before release. Thanks again!

@mbhoek mbhoek self-assigned this Dec 31, 2022
@mbhoek mbhoek added the bug Something isn't working label Dec 31, 2022
@mbhoek mbhoek added this to the Support SpecFlow v4 milestone Dec 31, 2022
@BlackGad
Copy link
Author

Thank you! But it is crucial issue for me right now :) Is there any chance to provide patch to current released version?

And happy new year!

@BlackGad
Copy link
Author

Btw do you know specflow 4 release date?

@mbhoek
Copy link
Member

mbhoek commented Dec 31, 2022

Unfortunately I won't be patching the current version, all my available time will be focused on the v4 release (first version is in preview right now). If you want to complete negate the disposing of classes I'd recommend trying v3.9.2. I have no knowledge of the Specflow v4 release timeline, I'm just trying to keep up with their betas.

Sorry for the inconvenience.

@304NotModified
Copy link
Contributor

304NotModified commented Jun 1, 2023

Is this a bug or a feature?

I'm trying to move from AdCodicem.SpecFlow.MicrosoftDependencyInjection, but I get mulitple scope/dispose issues which are hard to find out. (locally it works, on CI it fails)

@mbhoek
Copy link
Member

mbhoek commented Jun 1, 2023

@304NotModified Hmm, weird that it works locally but fails in CI -- sounds like a bug. Would you be willing to check against v3.9.2 and see if that changes anything? Migration from AdCodicem's plugin is still an outstanding issue (#86).

I am looking to fix most of the async and disposable problems with the release of SpecFlow v4 (they did a lot of work in there which I depend on), but unfortunately the release of v4 (or even the current state of SpecFlow) seems very unclear atm.

@mbhoek
Copy link
Member

mbhoek commented Feb 14, 2024

Closing this issue because the plugin will be donated to Reqnroll.

@mbhoek mbhoek closed this as completed Feb 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Development

No branches or pull requests

3 participants