Skip to content

mikeobrien/WebApi.StructureMap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Web API StructureMap Integration

Nuget TeamCity Build Status

This library integrates StructureMap with the Web API.

Installation

PM> Install-Package WebApi.StructureMap  

Usage

To register StructureMap, simply call the UseStructureMap<T>() extension method on startup, specifying your registry:

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        GlobalConfiguration.Configuration.UseStructureMap<Registry>();
        ...
    }
}

You can also configure StructureMap with the configuration DSL:

GlobalConfiguration.Configuration.UseStructureMap(x =>
{
    x.AddRegistry<Registry1>();
    x.AddRegistry<Registry2>();
});

The following objects are automatically injected into request scoped nested containers:

  • HttpRequestMessage
  • HttpControllerDescriptor
  • HttpRequestContext
  • IHttpRouteData

There are two convenience methods provided for HttpActionContext and HttpActionExecutedContext that simplify service location in action filters.

public class SomeFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        actionExecutedContext.GetService<SomeService>().DoSomething();
    }
}

public class SomeService
{
    public SomeService(
        HttpResponseMessage response,
        IDependency dependency) { ... }

    public void DoSomething() { ... }
}

These convenience methods will create an instance from the request scoped nested container. They will also pass the values of properties on HttpActionContext and HttpActionExecutedContext so your services can depend on them instead of HttpActionContext and HttpActionExecutedContext.

Source Injected
HttpActionExecutedContext HttpActionExecutedContext
HttpActionContext
HttpResponseMessage
HttpActionContext HttpActionContext
HttpActionDescriptor
HttpControllerContext
ModelStateDictionary

License

MIT License