Skip to content
On this page

Get a Service by ServiceType

Requesting the default configured object of a service type is done through the IContainer.GetInstance() method shown below:

cs
[Fact]
public void get_the_default_instance()
{
    var container = new Container(x => { x.For<IWidget>().Use<AWidget>(); });

    container.GetInstance<IWidget>()
        .ShouldBeOfType<AWidget>();

    // or

    container.GetInstance(typeof(IWidget))
        .ShouldBeOfType<AWidget>();
}

snippet source | anchor

cs
[Fact]
public void get_the_default_instance()
{
    var container = new Container(x => { x.For<IWidget>().Use<AWidget>(); });

    container.GetInstance<IWidget>()
        .ShouldBeOfType<AWidget>();

    // or

    container.GetInstance(typeof(IWidget))
        .ShouldBeOfType<AWidget>();
}

snippet source | anchor