All Projects → mikeobrien → WebApi.StructureMap

mikeobrien / WebApi.StructureMap

Licence: MIT license
Web API StructureMap Integration

Programming Languages

C#
18002 projects
powershell
5483 projects
javascript
184084 projects - #8 most used programming language
Classic ASP
548 projects

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].