Repository navigation
Description
Description
Description and repro for the problem are posted on Ninject project, however, there haven't been any active contributions that I can see in a while for Ninject so I'm wondering if I can get help narrowing down what the problem is.
I am getting the following call stack, which is strange ... if you emit IL, shouldn't the IL for .Net 5 be usable in .Net 6?
at System.Runtime.CompilerServices.RuntimeHelpers.CompileMethod(RuntimeMethodHandleInternal method)
at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType)
at Ninject.Injection.DynamicMethodInjectorFactory.Create(ConstructorInfo constructor)
at Ninject.Planning.Strategies.ConstructorReflectionStrategy.Execute(IPlan plan)
at Ninject.Planning.Planner.<>c__DisplayClass9_0.<CreateNewPlan>b__0(IPlanningStrategy s)
at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[T](IEnumerable`1 series, Action`1 action)
at Ninject.Planning.Planner.CreateNewPlan(Type type)
at Ninject.Planning.Planner.GetPlan(Type type)
at Ninject.Activation.Context.ResolveInternal(Object scope)
at Ninject.Activation.Context.Resolve()
at Ninject.KernelBase.Resolve(IRequest request, Boolean handleMissingBindings)
at Ninject.KernelBase.Resolve(IRequest request)
at Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique)
at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, String name, IParameter[] parameters)
at Example.MessageModule.<>c.<Load>b__0_1(IContext context) in C:\Users\nikos\Source\Repos\NinjectNet6\NinjectNet6\Program.cs:line 35
at Ninject.Parameters.Parameter.<>c__DisplayClass1_0.<.ctor>b__0(IContext ctx, ITarget target)
at Ninject.Parameters.Parameter.GetValue(IContext context, ITarget target)
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass15_0.<Create>b__0(ITarget target)
at System.Linq.Enumerable.SelectArrayIterator`2.ToArray()
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
at Ninject.Activation.Context.ResolveInternal(Object scope)
at Ninject.Activation.Context.Resolve()
at Ninject.KernelBase.Resolve(IRequest request, Boolean handleMissingBindings)
at Ninject.KernelBase.Resolve(IRequest request)
at Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique)
at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters)
at Example.Program.Main() in C:\Users\nikos\Source\Repos\NinjectNet6\NinjectNet6\Program.cs:line 14
Reproduction Steps
- Build a simple console app in .Net 6 with Ninject 3.3.4 dependency and the code from the issue report at .Net 6 + Ninject 3.3.4: InvalidProgramException when using delegates for initializing constructor parameters ninject/Ninject#389
- Press F5
You get InvalidProgramException
Expected behavior
The program should run with no exceptions as it does with .Net 5.
Actual behavior
InvalidProgramException.
Regression?
This seems to be a regression, unless there was some language design change.
Known Workarounds
There are no known workarounds. Replacing the DI container to something else is not really possible because most other DI frameworks don't support conditional resolution of constructor injection parameters.
Configuration
I am using .Net 6.0 as installed with Visual Studio 17.2.1.
Other information
No response
Activity
EgorBo commentedon Mar 30, 2022
signature - TYP_LONG
arg's real type - TYP_REF
the call is
System.String:.ctor(long,int,int):thisnschuessler commentedon Mar 30, 2022
This would have been the same case for .Net 5 correct or was there a behavior change?
jkotas commentedon Mar 30, 2022
Ninject is producing invalid IL using Reflection.Emit. The runtime behavior is unspecified for invalid IL. It may appear to "work", it may crash or it may corrupt the data. It switched from one behavior to another between major versions. It is not considered unacceptable breaking change.
This has to be fixed in Ninject.
EgorBo commentedon Mar 30, 2022
From a quick glance it feels like something is invoking
System.String:.ctor(long,int,int):thisand passes a GC reference to the first argumentEgorBo commentedon Mar 30, 2022
But overall Jan is right, it's just an invalid IL that has to be fixed where it was produced
EgorBo commentedon Mar 30, 2022
in .NET 6.0 we added an additional check via #43386
so I assume it worked for you in .NET 5.0 with a gc hole that could lead to crash at a random point
nschuessler commentedon Mar 30, 2022
should the
longthen bechar *. This one right: https://docs.microsoft.com/en-us/dotnet/api/system.string.-ctor?view=net-6.0#system-string-ctor(system-char*-system-int32-system-int32)basically, I can find the code that emits that but I don't know much about IL and what the IL change should be. What is the learning curve for the IL change that is required.
EgorBo commentedon Mar 30, 2022
I assume
longis actuallychar*it's just JIT displayed the signature like that. The problem that to thatchar*argument a managed reference was passed in the invalid ILEgorBo commentedon Mar 30, 2022
correct, from this IL it's something coming from an array and accessed via
ldelem.refeerhardt commentedon Mar 30, 2022
I'm going to close this issue as "external". The issue has moved to ninject/Ninject#389, as that is where the fix must be made.