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

BuildUp with InjectionMethod run-time configuration and Diagnostic() throws "Injected method could not be matched with any public methods" #306

Open
pascal-910 opened this issue Feb 26, 2021 · 0 comments

Comments

@pascal-910
Copy link

pascal-910 commented Feb 26, 2021

When using Diagnostic() Extension and a run-time configured InjectionMethod the following exception will be thrown when trying to BuildUp an existing Object.
"System.ArgumentException: Injected method SetDependencies() could not be matched with any public methods on type MyClassWithoutAttribute"
An alternatively usage of [InjectionMethod] Attribute however works as expected .

public interface ISomeInterface { }
public class SomeImpl : ISomeInterface { }

public class MyClassWithAttribute
{
    [InjectionMethod]
    public void SetDependencies(ISomeInterface some) { }
}

public class MyClassWithoutAttribute
{
    public void SetDependencies(ISomeInterface some) { }
}

[TestClass]
public class InjectionMethodIssue
{
    [TestMethod]
    // Usage of run-time configuration with 'InjectionMethod' throws an exception
    public void TestWithoutAttributeAndDiagnostic()
    {
        var container = new UnityContainer().AddExtension(new Diagnostic());
        container.RegisterType<ISomeInterface, SomeImpl>();
        container.RegisterType<MyClassWithoutAttribute>(new InjectionMethod("SetDependencies"));
        var instance = new MyClassWithoutAttribute();
        container.BuildUp(instance);
    }

    [TestMethod]
    // Usage of [InjectionMethod] Attribute instead run-time config works (OK)
    public void TestWithAttributeAndDiagnostic()
    {
        var container = new UnityContainer().AddExtension(new Diagnostic());
        container.RegisterType<ISomeInterface, SomeImpl>();
        var instance = new MyClassWithAttribute();
        container.BuildUp(instance);
    }

    [TestMethod]
    // Run-time config WITHOUT Diagnostic also works (OK)
    public void TestWithoutAttributeAndWithoutDiagnostic()
    {
        var container = new UnityContainer();
        container.RegisterType<ISomeInterface, SomeImpl>();
        container.RegisterType<MyClassWithoutAttribute>(new InjectionMethod("SetDependencies"));
        var instance = new MyClassWithoutAttribute();
        container.BuildUp(instance);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant