How to configure Web API 2 and Structure Map
Asked Answered
E

2

16

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;
        }
    }
}
Eldin answered 5/7, 2014 at 21:4 Comment(1)
Possible duplicate of WebAPI + APIController with structureMapMeuse
W
19

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

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));
    ...
Wriggler answered 6/7, 2014 at 15:57 Comment(1)
What already configured object factory.... Im being told it doesnt exist after downloading structuremapZonked
S
5

There is a nuget for this: https://www.nuget.org/packages/WebApi.StructureMap/

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        GlobalConfiguration.Configuration.UseStructureMap<Registry>();

        // Or

        GlobalConfiguration.Configuration.UseStructureMap(x =>
        {
            x.AddRegistry<Registry1>();
            x.AddRegistry<Registry2>();
        });

        ...
    }
}
Singleton answered 8/3, 2016 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.