Reqnroll icon indicating copy to clipboard operation
Reqnroll copied to clipboard

System.Collections.Generic.KeyNotFoundException : The given key 'Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope' was not present in the dictionary.

Open sanjeev-chevireddy opened this issue 1 year ago • 8 comments

Reqnroll Version

2.0.2

Which test runner are you using?

NUnit

Test Runner Version Number

4.0.1

.NET Implementation

.NET 6.0

Test Execution Method

Visual Studio Test Explorer

Content of reqnroll.json configuration file

No response

Issue Description

System.Collections.Generic.KeyNotFoundException : The given key 'Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope' was not present in the dictionary. TearDown : System.Collections.Generic.KeyNotFoundException : The given key 'Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope' was not present in the dictionary.

Stack Trace:  ConcurrentDictionary2.ThrowKeyNotFoundException(TKey key) ConcurrentDictionary2.get_Item(TKey key) <>c.<RegisterProxyBindings>b__9_21(IServiceProvider sp) CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) <59 more frames...> TestExecutionEngine.FireEventsAsync(HookType hookType) TestExecutionEngine.FireScenarioEventsAsync(HookType bindingEvent) TestExecutionEngine.OnScenarioEndAsync() TestRunner.OnScenarioEndAsync() CreateClaimFeature.TestTearDownAsync() GenericAdapter1.GetResult() AsyncToSyncAdapter.Await[TResult](Func1 invoke) AsyncToSyncAdapter.Await(Func1 invoke) SetUpTearDownItem.RunSetUpOrTearDownMethod(TestExecutionContext context, IMethodInfo method) SetUpTearDownItem.RunTearDown(TestExecutionContext context)

Steps to Reproduce

We are currently migrating from SpecFlow to ReQnRoll and have encountered the issue when running the tests after migrating from SolidToken.SpecFlow.DependencyInjection to Reqnroll.Microsoft.Extensions.DependencyInjection;

Please see our Startup method as shown below

using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Reqnroll.Microsoft.Extensions.DependencyInjection;

[ScenarioDependencies] public static IServiceCollection ConfigureServices() { var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); var connectionString = configuration["ConnectionString"];

IServiceCollection services = new ServiceCollection(); return services .AddSingleton(ConfigReader.GetBrowserSettingsData()) .AddSingleton(ConfigReader.GetAppSettingsData<AppSettings>()) .AddSingleton<IReportContext, ReportContext>() .AddSingleton<INotificationHelper, NotificationHelper>() .AddSingleton<IReportHelper, ReportHelper>() .AddSingleton<IPlaywrightDriver, PlaywrightDriver>() .AddSingleton<IHelper, Helper>() .AddSingleton<IApiContext, ApiContext>() .AddSingleton<IDataContext, DataContext>() .AddSingleton<ILoginPage, LoginPage>() .AddSingleton<IDbContainerFactory, CosmosContainerFactory>() .AddSingleton<IDbContainerFactory>(provider => { //Authorisation var client = GetCosmosClient(connectionString ); return new CosmosContainerFactory(client, "Claims"); }) }

Link to Repro Project

No response

sanjeev-chevireddy avatar Jun 11 '24 09:06 sanjeev-chevireddy

@mbhoek have you seen such error before?

gasparnagy avatar Jun 11 '24 09:06 gasparnagy

@gasparnagy It's a duplicate of https://github.com/solidtoken/SpecFlow.DependencyInjection/issues/88

@sanjeev-chevireddy Can I ask you, from which version of the SpecFlow plugin are you migrating from?

mbhoek avatar Jun 11 '24 14:06 mbhoek

I actually encountered this in a project as well. I wrote it up to something we did wrong and since we were moving to using solely Bodi I ignored the issue.

ajeckmans avatar Jun 11 '24 14:06 ajeckmans

@gasparnagy It's a duplicate of solidtoken/SpecFlow.DependencyInjection#88

@sanjeev-chevireddy Can I ask you, from which version of the SpecFlow plugin are you migrating from?

@mbhoek Specflow - 3.9.74, SolidToken.SpecFlow.DependencyInjection - 3.9.3

sanjeev-chevireddy avatar Jun 11 '24 16:06 sanjeev-chevireddy

Hmm, that surprises me because I've only ever seen this problem in v3.9.3 of the SpecFlow plugin. I'll investigate.

mbhoek avatar Jun 11 '24 22:06 mbhoek

I encountered the same error earlier but had to make it work quickly so I changed back to the default container. Later I wanted to reproduce to open an issue, started with a new project and it was working fine. 🤷

That project was based on the playwright example from the docs. Of course, replacing Autofac with MS DI, and using newer versions of most packages.

I will try to switch in that project again to see if I can get back the error.

mcraa avatar Jun 14 '24 08:06 mcraa

@mbhoek In my case, one of my dependencies needed a ScenarioContext in the constructor (like in the example). BoDi has that automatically but it is not there in the MS DI container.

mcraa avatar Jun 14 '24 10:06 mcraa

@mcraa Thanks that helps a lot! I'm trying to reproduce it in a unit test before solving it.

mbhoek avatar Jun 14 '24 21:06 mbhoek

Had the same issue when trying to inject ScenarioContext in a custom HttpMessageHandler.

rban-newday avatar Oct 25 '24 11:10 rban-newday

For reproducing the steps, @mbhoek, using the MS DI, when trying to get scenario context an error occurs:

      _services.AddHttpClient<TClient>()
            .ConfigureHttpClient((sp, client) =>
            {
                var env = sp.GetRequiredService<IOptions<EnvironmentConfig>>().Value;
            })
            .AddHttpMessageHandler(sp =>
            {
                var httpContext = sp.GetRequiredService<HttpRequestContext>();
                //This line below this comment: 
                var scenarioContext = sp.GetRequiredService<ScenarioContext>();

                return new HttpContextMessageHandler(httpContext, scenarioContext);
            })

Causes this:

System.Collections.Generic.KeyNotFoundException The given key 'Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope' was not present in the dictionary. at System.Collections.Concurrent.ConcurrentDictionary2.ThrowKeyNotFoundException(TKey key) at System.Collections.Concurrent.ConcurrentDictionary2.get_Item(TKey key) at Reqnroll.Microsoft.Extensions.DependencyInjection.DependencyInjectionPlugin.<>c.<RegisterProxyBindings>b__9_21(IServiceProvider sp)...

rban-newday avatar Nov 05 '24 11:11 rban-newday

Another repro for this at: https://github.com/reqnroll/Reqnroll/issues/247#issuecomment-2460231716

gasparnagy avatar Nov 07 '24 07:11 gasparnagy

I have analyzed the issue and found that this error is thrown when someone tries to access a test execution dependent service (e.g. IReqnrollOutputHelper) before the tests have started, e.g. in [BeforeTestRun] hook.

I have provided a fix to show a better error message, but in many cases the core problem is an incompatibility with SpecFlow that allowed to access these services in [BeforeTestRun] due to a bug (and it was only working for non-parallel execution). Anyway, that problem is discussed in #247 where there is also a workaround provided. The workaround needs to be tweaked though in some cases, see #317).

So this issue will be closed as we have a better error message once #318 is merged.

gasparnagy avatar Nov 07 '24 10:11 gasparnagy