Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Web API 2 and Structure Map

I've trawled through multiple blogs etc trying to find out how to configure StructureMap with Web API 2 and none of the implementations worked for me. The confusion seems to be around the different IDependency Resolver that MVC uses and the one that Web API uses.

Firstly, which is the correct Nuget package and secondly, how do you configure it with a pure Web API 2 project?

Thanks

This is what I have so far and it seems to be working. Is this correct?

 public class StructureMapControllerActivator : IHttpControllerActivator
{
    private readonly IContainer _container;

    public StructureMapControllerActivator(IContainer container)
    {
        if (container == null) throw new ArgumentNullException("container");
        _container = container;
    }

    public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    {
        try
        {
            var scopedContainer = _container.GetNestedContainer();
            scopedContainer.Inject(typeof(HttpRequestMessage), request);
            request.RegisterForDispose(scopedContainer);
            return (IHttpController)scopedContainer.GetInstance(controllerType);
        }
        catch (Exception e)
        {
            // TODO : Logging
            throw e;
        }
    }
}
like image 638
Greg Avatar asked Jul 05 '14 21:07

Greg


People also ask

How can Web API be configured?

Configuring Web API with ASP.NET Hosting In an ASP.NET application, configure Web API by calling GlobalConfiguration. Configure in the Application_Start method. The Configure method takes a delegate with a single parameter of type HttpConfiguration. Perform all of your configuration inside the delegate.

How do I add WebApiConfig to CS go?

In the Solution Explorer, select the folder App_Start and choose Add New Item... from the Project main menu. Select Class as the new item and name it WebApiConfig. cs. Confirm with Add.

What is WebApiConfig CS?

The WebApiConfig. cs is configuration file for Web API. You can configure routes and other things for web API, same like RouteConfig. cs is used to configure MVC routes. It also creates Web API controller ValuesController.

How do I add structuremap to a web API project?

Adding StructureMap. In the Web API project, let's add a NuGet package called StructureMap.WebApi2. On doing so, we'll see that the structure of the project has changed: The new files are what allow StructureMap to operate in our Web API project, and if you'd like more information about them, check out the GitHub documentation.

How do I configure Web API in ASP NET?

In an ASP.NET application, configure Web API by calling GlobalConfiguration.Configure in the Application_Start method. The Configure method takes a delegate with a single parameter of type HttpConfiguration. Perform all of your configuration inside the delegate. Here is an example using an anonymous delegate:

How do I use the route attribute with web API?

The Route attribute can be applied on any controller or action method. In order to use attribute routing with Web API, it must be enabled in WebApiConfig by calling config.MapHttpAttributeRoutes () method. Consider the following example of attribute routing.

What is webapiconfig in the project template?

The project template creates a file named WebApiConfig.cs inside the App_Start folder. This code file defines the delegate where you should put your Web API configuration code.


1 Answers

There are few links, were I've tried to explain how we can use (easily) StructureMap and Web API together:

  • WebAPI + APIController with structureMap
  • Configuring dependency injection with ASP.NET Web API 2.1

We have to implement the IHttpControllerActivator, which could profit from already configured StructureMap ObjectFactory:

public class ServiceActivator : IHttpControllerActivator
{
    public ServiceActivator(HttpConfiguration configuration) {}    

    public IHttpController Create(HttpRequestMessage request
        , HttpControllerDescriptor controllerDescriptor, Type controllerType)
    {
        var controller = ObjectFactory.GetInstance(controllerType) as IHttpController;
        return controller;
    }
}

And then, we have to just register our implementation (global.asax):

protected void Application_Start()
{
    ...
    HttpConfiguration config = GlobalConfiguration.Configuration;
    config.Services
          .Replace(typeof(IHttpControllerActivator), new ServiceActivator(config));
    ...
like image 153
Radim Köhler Avatar answered Oct 16 '22 09:10

Radim Köhler