This library integrates StructureMap with the Web API.
PM> Install-Package WebApi.StructureMap
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 |
MIT License