Skip to content

.Net 6 breaks Ninject compatability (InvalidProgramException) #67351

Closed

Description

@nschuessler

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

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

  1. ghost added
    untriaged
    on Mar 30, 2022
  2. EgorBo commented on Mar 30, 2022

    @EgorBo
    Member
    IL to import for DynamicClass:DynamicInjector7132ee88afc74b9589c0ac6b253986a9(System.Object[]):System.Object:
    IL_0000  02                ldarg.0
    IL_0001  16                ldc.i4.0
    IL_0002  9a                ldelem.ref
    IL_0003  74 02 00 00 02    castclass    0x2000002
    IL_0008  02                ldarg.0
    IL_0009  17                ldc.i4.1
    IL_000a  9a                ldelem.ref
    IL_000b  a5 03 00 00 02    unbox.any    0x2000003
    IL_0010  02                ldarg.0
    IL_0011  18                ldc.i4.2
    IL_0012  9a                ldelem.ref
    IL_0013  a5 04 00 00 02    unbox.any    0x2000004
    IL_0018  73 05 00 00 06    newobj       0x6000005   ;;   <---  the call argument has a type that can't be implicitly converted to the signature type
    IL_001d  2a                ret
    

    signature - TYP_LONG
    arg's real type - TYP_REF
    the call is System.String:.ctor(long,int,int):this

  3. nschuessler commented on Mar 30, 2022

    @nschuessler
    Author

    This would have been the same case for .Net 5 correct or was there a behavior change?

  4. jkotas commented on Mar 30, 2022

    @jkotas
    Member

    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.

  5. EgorBo commented on Mar 30, 2022

    @EgorBo
    Member

    From a quick glance it feels like something is invoking System.String:.ctor(long,int,int):this and passes a GC reference to the first argument

  6. EgorBo commented on Mar 30, 2022

    @EgorBo
    Member

    But overall Jan is right, it's just an invalid IL that has to be fixed where it was produced

  7. EgorBo commented on Mar 30, 2022

    @EgorBo
    Member

    This would have been the same case for .Net 5 correct or was there a behavior change?

    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

  8. nschuessler commented on Mar 30, 2022

    @nschuessler
    Author

    should the long then be char * . 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.

  9. EgorBo commented on Mar 30, 2022

    @EgorBo
    Member

    should the long then be char *

    I assume long is actually char* it's just JIT displayed the signature like that. The problem that to that char* argument a managed reference was passed in the invalid IL

  10. EgorBo commented on Mar 30, 2022

    @EgorBo
    Member

    should the long then be char *

    I assume long is actually char* it's just JIT displayed the signature like that. The problem that to that char* argument a managed reference was passed in the invalid IL

    correct, from this IL it's something coming from an array and accessed via ldelem.ref

  11. eerhardt commented on Mar 30, 2022

    @eerhardt
    Member

    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.

  12. ghost removed
    untriaged
    on Mar 30, 2022
  13. ghost locked as resolved and limited conversation to collaborators on Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions