Skip to content
Web API StructureMap Integration
C# PowerShell JavaScript ASP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
misc
src fix null reference in Extensions.cs (#5) Aug 13, 2018
.gitignore Fixed issues with tests. May 13, 2017
LICENSE Fixed issues with tests. May 13, 2017
README.md Fixed issues with tests. May 13, 2017
WebApi.StructureMap.nuspec Changed StructureMap version dependency in nuspec Oct 26, 2018
gulpfile.js Fixed and issue with the build script. Jun 27, 2017
package.json Fixed and issue with the build script. Jun 27, 2017

README.md

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

You can’t perform that action at this time.