Skip to content
On this page

Get all Services by Service Type

WARNING

The functionality respects the order in which the actual instances are configured in the Container -- which is compliant with the expected behavior inside of ASP.Net Core. Be warned that some other IoC tools make different assumptions if you are coming from a different tool.

Please see working with Enumerable Types for a lot more information about what's going on behind the scenes.

Once in a while you might want to get an enumerable of all the configured objects for a ServiceType. That's done with the GetAllInstances() method shown below:

cs
[Fact]
public void get_all_instances()
{
    var container = new Container(x =>
    {
        x.For<IWidget>().Add<AWidget>().Named("A");
        x.For<IWidget>().Add<BWidget>().Named("B");
        x.For<IWidget>().Add<CWidget>().Named("C");
    });

    container.QuickBuildAll<IWidget>()
        .Select(x => x.GetType())
        .ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(BWidget), typeof(CWidget));

    container.GetAllInstances<IWidget>()
        .Select(x => x.GetType())
        .ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(BWidget), typeof(CWidget));

    // or

    container.GetAllInstances(typeof(IWidget))
        .OfType<IWidget>() // returns an IEnumerable, so I'm casting here
        .Select(x => x.GetType())
        .ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(BWidget), typeof(CWidget));
}

snippet source | anchor

cs
[Fact]
public void get_all_instances()
{
    var container = new Container(x =>
    {
        x.For<IWidget>().Add<AWidget>().Named("A");
        x.For<IWidget>().Add<BWidget>().Named("B");
        x.For<IWidget>().Add<CWidget>().Named("C");
    });

    container.GetAllInstances<IWidget>()
        .Select(x => x.GetType())
        .ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(BWidget), typeof(CWidget));

    // or

    container.GetAllInstances(typeof(IWidget))
        .OfType<IWidget>() // returns an IEnumerable, so I'm casting here
        .Select(x => x.GetType())
        .ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(BWidget), typeof(CWidget));
}

snippet source | anchor