Skip to content

WhatDoIHave() ​

The IContainer.WhatDoIHave() method can give you a quick textual report of the current configuration of a running Container:

cs
var container = Container.Empty();
var report = container.WhatDoIHave();

Console.WriteLine(report);

snippet source | anchor

cs
var container = new Container();
var report = container.WhatDoIHave();

Debug.WriteLine(report);

snippet source | anchor

Enough talk, say you have a Container with this configuration:

cs
var container = new Container(x =>
{
    x.For<IEngine>().Use<Hemi>().Named("The Hemi");

    x.For<IEngine>().Add<VEight>().Singleton().Named("V8");
    x.For<IEngine>().Add<FourFiftyFour>();
    x.For<IEngine>().Add<StraightSix>().Scoped();

    x.For<IEngine>().Add(c => new Rotary()).Named("Rotary");
    x.For<IEngine>().Add(c => c.GetService<PluginElectric>());

    x.For<IEngine>().Add(new InlineFour());

    x.For<IEngine>().UseIfNone<VTwelve>();
});

snippet source | anchor

cs
var container = new Container(x =>
{
    x.For<IEngine>().Use<Hemi>().Named("The Hemi");

    x.For<IEngine>().Add<VEight>().Singleton().Named("V8");
    x.For<IEngine>().Add<FourFiftyFour>().AlwaysUnique();
    x.For<IEngine>().Add<StraightSix>().LifecycleIs<ThreadLocalStorageLifecycle>();

    x.For<IEngine>().Add(() => new Rotary()).Named("Rotary");
    x.For<IEngine>().Add(c => c.GetInstance<PluginElectric>());

    x.For<IEngine>().Add(new InlineFour());

    x.For<IEngine>().UseIfNone<VTwelve>();
    x.For<IEngine>().MissingNamedInstanceIs.ConstructedBy(c => new NamedEngine(c.RequestedName));
});

snippet source | anchor

If you were to run the code below against this Container:

cs
Console.WriteLine(container.WhatDoIHave());

snippet source | anchor

cs
Debug.WriteLine(container.WhatDoIHave());

snippet source | anchor

you would get the output shown in this gist.

If you're curious, all the raw code for this example is in here.

Filtering WhatDoIHave() ​

Filtering the WhatDoIHave() results can be done in these ways:

cs
var container = Container.Empty();

// Filter by the Assembly of the Service Type
var byAssembly = container.WhatDoIHave(assembly: typeof(IWidget).Assembly);

// Only report on the specified Service Type
var byServiceType = container.WhatDoIHave(typeof(IWidget));

// Filter to Service Type's in the named namespace
// The 'IsInNamespace' test will include child namespaces
var byNamespace = container.WhatDoIHave(@namespace: "StructureMap.Testing.Widget");

// Filter by a case insensitive string.Contains() match
// against the Service Type name
var byType = container.WhatDoIHave(typeName: "Widget");

snippet source | anchor

cs
var container = new Container();

// Filter by the Assembly of the Plugin Type
var byAssembly = container.WhatDoIHave(assembly: typeof(IWidget).GetAssembly());

// Only report on the specified Plugin Type
var byPluginType = container.WhatDoIHave(typeof(IWidget));

// Filter to Plugin Type's in the named namespace
// The 'IsInNamespace' test will include child namespaces
var byNamespace = container.WhatDoIHave(@namespace: "StructureMap.Testing.Widget");

// Filter by a case insensitive string.Contains() match
// against the Plugin Type name
var byType = container.WhatDoIHave(typeName: "Widget");

snippet source | anchor

WhatDoIHave() under ASP.Net Core ​

You can call WhatDoIHave() and WhatDidIScan() when running in ASP.Net Core like so:

cs
public class StartupWithDiagnostics
{
    // Take in Lamar's ServiceRegistry instead of IServiceCollection
    // as your argument, but fear not, it implements IServiceCollection
    // as well
    public void ConfigureContainer(ServiceRegistry services)
    {
        // Supports ASP.Net Core DI abstractions
        services.AddMvc();
        services.AddLogging();

        // Also exposes Lamar specific registrations
        // and functionality
        services.Scan(s =>
        {
            s.TheCallingAssembly();
            s.WithDefaultConventions();
        });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if(env.IsDevelopment())
        {
            var container = (IContainer)app.ApplicationServices;
            // or write to your own Logger
            Console.WriteLine(container.WhatDidIScan());
            Console.WriteLine(container.WhatDoIHave());
        }

        app.UseMvc();
    }
}

snippet source | anchor