Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when using Autofac Plugin with injection to a static methods #2307

9 of 38 tasks
HotTaton opened this issue Feb 11, 2021 · 4 comments
9 of 38 tasks

Crash when using Autofac Plugin with injection to a static methods #2307

HotTaton opened this issue Feb 11, 2021 · 4 comments


Copy link

HotTaton commented Feb 11, 2021

Crash when using dependency injection in static methods (i.e. hooks marked AfterFeature attribute)

SpecFlow Version:

  • 3.6
  • 3.5
  • 3.4
  • 3.3
  • 3.1
  • 3.0
  • 2.4
  • 2.3
  • 2.2
  • 2.1
  • 2.0
  • 1.9

Used Test Runner

  • SpecFlow+Runner
  • MSTest
  • NUnit
  • Xunit

Version number:

Project Format of the SpecFlow project

  • Classic project format using packages.config
  • Classic project format using <PackageReference> tags
  • Sdk-style project format

.feature.cs files are generated using

  • SpecFlow.Tools.MsBuild.Generation NuGet package
  • SpecFlowSingleFileGenerator custom tool

Visual Studio Version

  • VS 2019
  • VS 2017

Enable SpecFlowSingleFileGenerator Custom Tool option in Visual Studio extension settings

  • Enabled
  • Disabled

Are the latest Visual Studio updates installed?

  • Yes
  • No, I use Visual Studio version <Major>.<Minor>.<Patch>

.NET Framework:

  • >= .NET 4.5
  • before .NET 4.5
  • .NET Core 2.0
  • .NET Core 2.1
  • .NET Core 2.2
  • .NET Core 3.0
  • .NET Core 3.1
  • .NET 5.0

Test Execution Method:

  • Visual Studio Test Explorer

<SpecFlow> Section in app.config or content of specflow.json

Issue Description

Crash when using dependency injection in static methods (i.e. hooks marked AfterFeature attribute).
Autofac container created for Scenario and when time comes to resolve injection in static method it is already disposed.

Steps to Reproduce

You can reproduce it with creating hook with code below:

public static void AfterFeature(FeatureContext featureContext)
    var item = some.FeatureInfo;
Copy link
Contributor Author

HotTaton commented Feb 11, 2021

Code below can solve some cases (injecting FeatureContext for example, but it not cover all scenarios. I think maybe we need a new attribute, FeatureDependencies (with logic like in ScenarioDependencies) or something like that.
I can contribute code below if all good, or I can help with resolving problem generally.

public class AutofacTestObjectResolver : ITestObjectResolver
        public object ResolveBindingInstance(Type bindingType, IObjectContainer scenarioContainer)
            if (scenarioContainer.IsRegistered<IComponentContext>())
                var componentContext = scenarioContainer.Resolve<IComponentContext>();
                return componentContext.Resolve(bindingType);
            return scenarioContainer.Resolve(bindingType);


Copy link

Ah, yes, that is a problem.
We want to resolve the FeatureContext from the Autofac container, which doesn't work because it is not registered there.

If you could provide a PR with a fix for it, that would be awesome and probably the quickest way to get it fixed.
As we are only a small team, it will probably take some time until we get to this issue.

If you need help, you can join the #contributing channel on our discord server (

Copy link

Fixed with #2315

RuslanUvarov pushed a commit to RuslanUvarov/SpecFlow that referenced this issue Feb 24, 2021
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

No branches or pull requests

2 participants