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

gds03
  • 1,349
  • 3
  • 18
  • 39

2 Answers2

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));
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Greate ;) I am happy with that as well ;) – Radim Köhler Sep 25 '13 at 09:28
  • 5
    In my own app it was `ObjectFactory.GetInstance` instead of `Factory.CreateInstance` – Akkuma Oct 02 '13 at 20:29
  • *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...* – Radim Köhler Oct 03 '13 at 10:47
  • ObjectFactory is marked as obsolete, but Container.For() returns an error because there is not a parameterless constructor what are my options? – Brian Nov 14 '14 at 16:26
  • 1
    @Brian *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... – Radim Köhler Nov 15 '14 at 04:53
  • 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`)* – Radim Köhler Mar 27 '15 at 15:00
  • 1
    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. – pgk Mar 27 '15 at 16:02
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.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258