WARNING: Unfortunately the implementation below is flawed and can't be used in a self hosted Web API. Please see here for more information.

ASP.NET Web API Integration Guide for Self Hosted Environments

When running ASP.NET Web API in a self hosted environment, there is no HttpContext, which means that Per Request lifestyle lifestyles can not be used. In that case the Per Lifetime Scope lifestyle can be used instead, but this requires a IDependencyResolver that uses scoping (since Per Lifetime Scope is thread-specific, it can't be wrapped around a HTTP request, since ASP.NET can finish the request on a different thread than where it was started).

The following code snippet defines a SelfHostedSimpleInjectorWebApiDependencyResolver class for a self hosted ASP.NET Web API application. You can create a new C# file in your Web API project and copy paste the code below into it.

using System;
using System.Collections.Generic;
using System.Web.Http.Dependencies;

using SimpleInjector;
using SimpleInjector.Extensions.LifetimeScoping;

public class SelfHostedSimpleInjectorWebApiDependencyResolver
    : IDependencyResolver
{
    private readonly Container container;
    private readonly LifetimeScope lifetimeScope;

    public SelfHostedSimpleInjectorWebApiDependencyResolver(
        Container container)
        : this(container, false)
    {
    }

    private SelfHostedSimpleInjectorWebApiDependencyResolver(
        Container container, bool createScope)
    {
        this.container = container;

        if (createScope)
        {
            this.lifetimeScope = container.BeginLifetimeScope();
        }
    }

    public IDependencyScope BeginScope()
    {
        return new SelfHostedSimpleInjectorWebApiDependencyResolver(
            this.container, true);
    }

    public object GetService(Type serviceType)
    {
        return ((IServiceProvider)this.container)
            .GetService(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return this.container.GetAllInstances(serviceType);
    }

    public void Dispose()
    {
        if (this.lifetimeScope != null)
        {
            this.lifetimeScope.Dispose();
        }
    }
}

The following code snippet shows how to use the SelfHostedSimpleInjectorWebApiDependencyResolver extension method.

// Create the container as usual.
var container = new Container();

// Register your types, for instance:
container.Register<IUserRepository, SqlUserRepository>();
container.RegisterLifetimeScope<IUnitOfWork, EfUnitOfWork>();

// Create your self-host configuration
var configuration = new HttpSelfHostConfiguration(uriBuilder.Uri);

configuration.Routes.MapHttpRoute(
	name: "Default Route",
	routeTemplate: "api/{controller}/{id}",
	defaults: new { id = RouteParameter.Optional });

configuration.DependencyResolver =
	new SelfHostedSimpleInjectorWebApiDependencyResolver(container);

var server = new HttpSelfHostServer(configuration);

// Start the server etc...

Last edited Aug 11, 2014 at 9:38 AM by dot_NET_Junkie, version 5