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

System.InvalidOperationException when UseMvc is called in Release mode #77

Closed
LordZoltan opened this Issue Feb 3, 2019 · 0 comments

Comments

Projects
None yet
1 participant
@LordZoltan
Copy link
Member

LordZoltan commented Feb 3, 2019

With the default configuration of Rezolver in Asp.Net Core as per the documentation, you might encounter this issue (note - only first few lines of Stack trace displayed is it's quite deep:

System.InvalidOperationException: ValueFactory attempted to access the Value property of this instance.
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at Rezolver.Targets.SingletonTarget.SingletonContainer.<>c__DisplayClass8_0`1.<GetObject>b__0(TypeAndTargetId k)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Rezolver.Targets.SingletonTarget.SingletonContainer.GetObject[TCompileContext](TCompileContext context, TypeAndTargetId key, Func`2 compiledTargetFactory) in E:\Github\Rezolver\src\Rezolver\Targets\SingletonTarget.cs:line 63
   at Rezolver.Targets.SingletonTarget.SingletonContainer.GetObject[TCompileContext](TCompileContext context, Guid targetId, Func`2 compiledTargetFactory) in E:\Github\Rezolver\src\Rezolver\Targets\SingletonTarget.cs:line 57
   at Rezolver.Compilation.Expressions.SingletonTargetBuilder.Build(SingletonTarget target, IExpressionCompileContext context, IExpressionCompiler compiler)
   at Rezolver.Compilation.Expressions.ExpressionBuilderBase`1.Build(ITarget target, IExpressionCompileContext context, IExpressionCompiler compiler) in E:\Github\Rezolver\src\Rezolver\Compilation\Expressions\ExpressionBuilderBase`1.cs:line 68
   at Rezolver.Compilation.Expressions.ExpressionBuilderBase.BuildCore(ITarget target, IExpressionCompileContext context, IExpressionCompiler compiler) in E:\Github\Rezolver\src\Rezolver\Compilation\Expressions\ExpressionBuilderBase.cs:line 199
   at Rezolver.Compilation.Expressions.ExpressionBuilderBase.Rezolver.Compilation.Expressions.IExpressionBuilder.Build(ITarget target, IExpressionCompileContext context, IExpressionCompiler compiler) in E:\Github\Rezolver\src\Rezolver\Compilation\Expressions\ExpressionBuilderBase.cs:line 321
   at Rezolver.Compilation.Expressions.ExpressionCompiler.Build(ITarget target, IExpressionCompileContext context) in E:\Github\Rezolver\src\Rezolver\Compilation\Expressions\ExpressionCompiler.cs:line 221
   at Rezolver.Compilation.Expressions.VariantMatchTargetBuilder.Build(VariantMatchTarget target, IExpressionCompileContext context, IExpressionCompiler compiler) in E:\Github\Rezolver\src\Rezolver\Compilation\Expressions\VariantMatchTargetBuilder.cs:line 26
   at Rezolver.Compilation.Expressions.ExpressionBuilderBase`1.Build(ITarget target, IExpressionCompileContext context, IExpressionCompiler compiler) in E:\Github\Rezolver\src\Rezolver\Compilation\Expressions\ExpressionBuilderBase`1.cs:line 68
[...]

I haven't done a deep check of the exact reason, but the issue is caused by some ambiguous registrations made by the Asp.Net Core MVC library against the EndpointDataSource type or one of its derivatives.

That is - they are ambiguous when the container is configured to pick up registrations for enumerables covariantly by default (which Rezolver is).

The simplest workaround is to add a callback to your UseRezolver call in Program.cs as follows:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
  WebHost.CreateDefaultBuilder(args)
    .UseRezolver(o => o.TargetContainerConfig.ConfigureOption<EnableEnumerableCovariance, EndpointDataSource>(false))
    .UseStartup<Startup>();

This ensures the covariance is disabled when the container is asked for an IEnumerable<EndpointDataSource> such that only registrations made specifically against the type EndpointDataSource are considered for inclusion in the enumerable.

The next version of the Rezolver.Microsoft.AspNetCore.Hosting library will automatically apply this configuration, so you won't have to.

@LordZoltan LordZoltan added the bug label Feb 3, 2019

@LordZoltan LordZoltan added this to the 1.4 milestone Feb 3, 2019

@LordZoltan LordZoltan added in progress and removed in progress labels Feb 11, 2019

LordZoltan added a commit that referenced this issue Feb 22, 2019

Autofactories & Lazies (1.4) (#75)
# Rezolver 1.4

- Implements #80 
- Implements #38 
- Implements #39 
- Fixes #77
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.