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

Combining RegisterDelegate with TrackingDisposableTransients rule throws TargetParameterCountException #315

Closed
zireael-0 opened this issue Sep 7, 2020 · 1 comment
Labels
bug Something isn't working
Milestone

Comments

@zireael-0
Copy link

This is a follow-up to #314

I was trying to use RegisterDelegate<DbContext>(c => c.Resolve<ConcreteDbContext>()) which failed with a reflection exception. I have finally been able to reproduce it, even though I am not affected by it anymore thanks to #314

Here is a minimal broken example:

namespace Playground
{
  using System;
  using DryIoc;

  public class Foo : IDisposable
  {
    public void Dispose() { Console.WriteLine("Disposed of `Foo` instance"); }
  }

  public class Bar : Foo { }

  public static class Program
  {
    public static void Main()
    {
      var container = new Container(rules => rules.WithTrackingDisposableTransients());
      container.RegisterDelegate<Foo>(c => new Bar());
      container.Resolve<Foo>();
    }
  }
}

The rule WithTrackingDisposableTransients seems to be enabled by default, when using new DryIocServiceProviderFactory(new Container())) adapter for MsDI.

Foo represents the DbContext while Bar represents a concrete implementation of it.

With this combination, during resolving, the convert expression is no longer recognized as DelegateExpression in Interpreter.TryInterpret -> switch(expr.NodeType):ExprType.Convert. Because of that, the expression is interpreted as a usual Invoke Expression in the resulting TryInterpret call.

For some reason, the RuntimeMethodInfo created from Delegate.GetMethodInfo() declares the one parameter, that is actually used within the delegate. However, the Delegate.Method property, declares two parameters: First the delegate function, second the resolver context. As far as I can tell, this is the ToFactoryDelegate extension method declared in Registrator. Unfortunately, this is also the method actually being invoked - resulting in a TargetParameterCountException

Using .NET Core for Linux on Microsoft.NETCore.App 3.1.7 using dotnet-sdk 3.1.107

@dadhi
Copy link
Owner

dadhi commented Sep 7, 2020

@linkipinki Special thanks for dissecting it! Will look.

@dadhi dadhi added the bug Something isn't working label Sep 7, 2020
@dadhi dadhi added this to the v4.4.1 milestone Sep 7, 2020
dadhi pushed a commit that referenced this issue Sep 9, 2020
@dadhi dadhi closed this as completed in 8892117 Sep 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants