z4kn4fein / stashbox-extensions-dependencyinjection

Stashbox Integration for ASP.NET Core, .NET Generic Host and ServiceCollection based applications.
https://z4kn4fein.github.io/stashbox
MIT License
17 stars 2 forks source link

Unable to resolve type 'Microsoft.Extensions.Options.OptionsMonitor`1[[Microsoft.Identity.Web.MicrosoftIdentityOptions, Microsoft.Identity.Web.TokenAcquisition]] #16

Closed schuettecarsten closed 1 month ago

schuettecarsten commented 1 month ago

Use Visual Studio 2022 to create a new "ASP.NET Core Web App (Model-View-Controller)" application that uses "Microsoft Identity Platform" for authentication. Select an existing App Registration or create a new one, that does not matter. Do not change anything to the generated project and make sure that it works.

Now add Stashbox and the following lines to the Main method after var builder = WebApplication.CreateBuilder(args);:

            // Use Stashbox (https://github.com/z4kn4fein/stashbox) as service provider
            builder.Host.UseStashbox(container => // Optional configuration options.
            {
                container.Configure(options => { /*...*/ });
            });
            builder.Host.ConfigureContainer<IStashboxContainer>((context, container) =>
            {
                container.Validate();
            });

The application will now crash with the following exception:

Stashbox.Exceptions.ResolutionFailedException: Unable to resolve type 'Microsoft.Extensions.Options.OptionsMonitor`1[[Microsoft.Identity.Web.MicrosoftIdentityOptions, Microsoft.Identity.Web.TokenAcquisition, Version=2.19.1.0, Culture=neutral, PublicKeyToken=0a613f4dd989e8ae]]' with name 'The service Microsoft.Extensions.Options.OptionsMonitor`1[Microsoft.Identity.Web.MicrosoftIdentityOptions] was unavailable after 3000 ms. It's possible that the thread used to construct it crashed by a handled exception.This exception is supposed to prevent other caller threads from infinite waiting for service construction.'.
Service is not registered properly or unresolvable type requested.
   at Stashbox.ResolutionScope.ScopedEvaluator.WaitForEvaluation(Type serviceType)
   at Stashbox.ResolutionScope.ScopedEvaluator.Evaluate(IResolutionScope scope, IRequestContext requestContext, Func`3 factory, Type serviceType)
   at Stashbox.ResolutionScope.GetOrAddScopedObject(Int32 key, Func`3 factory, IRequestContext requestContext, Type serviceType)
   at Stashbox.Lifetime.SingletonLifetime.ApplyLifetime(Func`3 factory, ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Lifetime.FactoryLifetimeDescriptor.BuildLifetimeAppliedExpression(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Lifetime.LifetimeDescriptor.ApplyLifetime(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionAndApplyLifetime(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation, LifetimeDescriptor secondaryLifetimeDescriptor)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionForRegistration(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionForType(ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.ResolutionScope.BuildAndResolveService(Type type, Object name, Object[] dependencyOverrides, ResolutionBehavior resolutionBehavior)
   at Stashbox.ResolutionScope.Resolve(Type typeFrom)
   at Stashbox.Extensions.DependencyInjection.StashboxServiceProvider.GetRequiredService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.Options.OptionsBuilder`1.<>c__DisplayClass12_0`4.<Configure>b__0(IServiceProvider sp)
   at (Closure, IResolutionScope, IRequestContext)
   at Stashbox.ResolutionScope.ScopedEvaluator.Evaluate(IResolutionScope scope, IRequestContext requestContext, Func`3 factory, Type serviceType)
   at Stashbox.ResolutionScope.GetOrAddScopedObject(Int32 key, Func`3 factory, IRequestContext requestContext, Type serviceType)
   at Stashbox.Lifetime.SingletonLifetime.ApplyLifetime(Func`3 factory, ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Lifetime.FactoryLifetimeDescriptor.BuildLifetimeAppliedExpression(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Lifetime.LifetimeDescriptor.ApplyLifetime(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionAndApplyLifetime(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation, LifetimeDescriptor secondaryLifetimeDescriptor)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionForRegistration(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionForType(ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.ResolutionScope.BuildAndResolveService(Type type, Object name, Object[] dependencyOverrides, ResolutionBehavior resolutionBehavior)
   at Stashbox.ResolutionScope.Resolve(Type typeFrom)
   at Stashbox.Extensions.DependencyInjection.StashboxServiceProvider.GetRequiredService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.Options.OptionsBuilder`1.<>c__DisplayClass12_0`4.<Configure>b__0(IServiceProvider sp)
   at (Closure, IResolutionScope, IRequestContext)
   at Stashbox.ResolutionScope.ScopedEvaluator.Evaluate(IResolutionScope scope, IRequestContext requestContext, Func`3 factory, Type serviceType)
   at Stashbox.ResolutionScope.GetOrAddScopedObject(Int32 key, Func`3 factory, IRequestContext requestContext, Type serviceType)
   at Stashbox.Lifetime.SingletonLifetime.ApplyLifetime(Func`3 factory, ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Lifetime.FactoryLifetimeDescriptor.BuildLifetimeAppliedExpression(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Lifetime.LifetimeDescriptor.ApplyLifetime(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionAndApplyLifetime(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation, LifetimeDescriptor secondaryLifetimeDescriptor)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionForRegistration(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionForType(ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Expressions.ExpressionFactory.TryBuildMethod(MethodBase method, ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation, TypeInformation& failedParameter, Expression[]& parameterExpressions)
   at Stashbox.Expressions.ExpressionFactory.SelectConstructor(Type typeToConstruct, ServiceRegistration serviceRegistration, TypeInformation typeInformation, ResolutionContext resolutionContext, IEnumerable`1 constructorsEnumerable, Expression[]& parameterExpressions)
   at Stashbox.Expressions.ExpressionFactory.CreateInitExpression(Type typeToConstruct, ServiceRegistration serviceRegistration, TypeInformation typeInformation, ResolutionContext resolutionContext, IEnumerable`1 constructors)
   at Stashbox.Expressions.ExpressionFactory.ConstructExpression(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Expressions.ExpressionBuilder.PrepareDefaultExpression(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Expressions.ExpressionBuilder.GetExpressionForDefault(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Expressions.ExpressionBuilder.BuildExpressionByRegistrationType(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Expressions.ExpressionBuilder.BuildExpressionForRegistration(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Lifetime.LifetimeDescriptor.GetExpressionForRegistration(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Lifetime.ExpressionLifetimeDescriptor.BuildLifetimeAppliedExpression(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Lifetime.LifetimeDescriptor.ApplyLifetime(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionAndApplyLifetime(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation, LifetimeDescriptor secondaryLifetimeDescriptor)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionForRegistration(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.Resolution.ResolutionStrategy.BuildExpressionForType(ResolutionContext resolutionContext, TypeInformation typeInformation)
   at Stashbox.ResolutionScope.BuildAndResolveServiceOrDefault(Type type, Object name, Object[] dependencyOverrides, ResolutionBehavior resolutionBehavior)
   at Stashbox.ResolutionScope.ResolveOrDefault(Type typeFrom)
   at Stashbox.Extensions.DependencyInjection.StashboxServiceProvider.GetService(Type serviceType)
   at Microsoft.AspNetCore.Authentication.AuthenticationHandlerProvider.GetHandlerAsync(HttpContext context, String authenticationScheme)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

This, of course, should not happen.

z4kn4fein commented 1 month ago

Hi @schuettecarsten, I've released a new version from the extensions v5.5.4, which fixes this issue. Let me know if you have further issues.

schuettecarsten commented 1 month ago

Fixed, thank you!