Skip to content

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

@pascal-910

Description

@pascal-910

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions