SimpleInjectorWebApiDependencyResolver Class

Simple Injector
Simple Injector IDependencyResolver implementation.
Inheritance Hierarchy

SystemObject
  SimpleInjector.Integration.WebApiSimpleInjectorWebApiDependencyResolver

Namespace:  SimpleInjector.Integration.WebApi
Assembly:  SimpleInjector.Integration.WebApi (in SimpleInjector.Integration.WebApi.dll) Version: 5.3.0
Syntax

public sealed class SimpleInjectorWebApiDependencyResolver : IDependencyResolver, 
	IDependencyScope, IDisposable

The SimpleInjectorWebApiDependencyResolver type exposes the following members.

Constructors

  NameDescription
Public methodSimpleInjectorWebApiDependencyResolver(Container)
Initializes a new instance of the SimpleInjectorWebApiDependencyResolver class with the default scope option (i.e. to use an ambient AsyncScopedLifestyle scope if one already exists).
Public methodSimpleInjectorWebApiDependencyResolver(Container, DependencyResolverScopeOption)
Initializes a new instance of the SimpleInjectorWebApiDependencyResolver class.
Top
Methods

  NameDescription
Public methodDispose
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Top
Explicit Interface Implementations

  NameDescription
Explicit interface implementationPrivate methodIDependencyResolver.BeginScope
Starts a resolution scope.
Explicit interface implementationPrivate methodIDependencyScope.GetService
Retrieves a service from the scope.
Explicit interface implementationPrivate methodIDependencyScope.GetServices
Retrieves a collection of services from the scope.
Top
Examples

The following example shows the usage of the SimpleInjectorWebApiDependencyResolver in an Web API application:
C#
using System.Web.Http;
using SimpleInjector;
using SimpleInjector.Integration.WebApi;

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        var container = new Container();

        // Make the container registrations, example:
        // container.Register<IUserRepository, SqlUserRepository>();

        container.RegisterWebApiControllers(config);
        container.RegisterWebApiFilterProvider(config);

        // Create a new SimpleInjectorDependencyResolver that wraps the,
        // container, and register that resolver in MVC.

        container.Verify();

        config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
The previous example show the use of the RegisterWebApiControllers extension methods and how the SimpleInjectorWebApiDependencyResolver can be used to set the created Container instance as default dependency resolver in Web API.
See Also

Reference