container icon indicating copy to clipboard operation
container copied to clipboard

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

Open pascal-910 opened this issue 5 years ago • 0 comments

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);
    }
}

pascal-910 avatar Feb 26 '21 09:02 pascal-910