WebAPI + APIController with structureMap
Asked Answered
O

2

22

Im trying to use StructureMap to initialize my ValuesController that derivate from ApiController but i'm getting an exception that says:

The IControllerFactory '...CustomControllerFactory' did not return a controller for the name 'api'.

Here is the code..

public class CustomControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
            return null;

        return (Controller)ConcretizationsManager.GetInstance(controllerType);
    }
}

basically ConcretizationsManager is a wrapper of StructureMap.. This method always runs ok for Controllers of Controller type, but for APIController dont.

Anyone to get me to the right direction? Very thanks

Olibanum answered 19/9, 2013 at 13:59 Comment(0)
D
39

For the Web API I am using Custom ServiceActivator like this (i.e. different factory then for MVC):

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

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

Where Factory is my wrapper of the Sturcturemap API

And in the App_Start\WebApiConfig.cs I do register that like this:

config.Services.Replace(typeof(IHttpControllerActivator), new ServiceActivator(config));
Diversified answered 25/9, 2013 at 3:4 Comment(7)
Greate ;) I am happy with that as well ;)Suavity
In my own app it was ObjectFactory.GetInstance instead of Factory.CreateInstanceTrews
Just a note, you are right. We are using the Abstraction, Custom Factory, with its provider, wrapping the StructureMap. But it could be as you say...Suavity
ObjectFactory is marked as obsolete, but Container.For<IHttpController>() returns an error because there is not a parameterless constructor what are my options?Sabaean
@Sabaean You should issue your own question to get answer how to use container... I am for example using Custom IRegistry MyRegistry : Registry which is place for all settings. Then I can build new Container(new MyRegistry()); and even more of them...Suavity
I am running 5.2.2 and SM 3.1.4.143 - all is working and defined above (and also, behind custom Factory are few SM StructureMap.Container instead of obsolete ObjectFactory)Suavity
I just had to implement WebApi 5.2.2 on top of MVC 5.2.2 so I just used package "StructureMap.WebApi2" which worked out of the box. There is no need for custom code. If you have *catch all route in MVC make sure you register WebAPI (routes) BEFORE MVC ones. I just got stuck for a while getting 404s instead of hitting WebAPI and though problem was with DI.Superlative
L
2

There is first party support for this with StructureMap 3

nuget install Structuremap.WebApi2

StructureMap 4 has been released and could be used also, however there may be slight tweaks needed. I have not confirmed for myself.

Lidda answered 3/12, 2015 at 19:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.