SkillAgentSearch skills...

WebApi.StructureMap

Web API StructureMap Integration

Install / Use

/learn @mikeobrien/WebApi.StructureMap
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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

View on GitHub
GitHub Stars12
CategoryDevelopment
Updated6y ago
Forks5

Languages

C#

Security Score

75/100

Audited on Aug 14, 2019

No findings