Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'Api.Controllers.' does not have a default constructor

I have an controller that use dependency injection:

   public FeedController(IFeedProcessor feedProcessor)
        {
            _feedProcessor = feedProcessor;
        }

This my config code:

public static void Config()
    {

        ObjectFactory.Initialize(x => x.Scan(scan =>
            {
                x.For<IFeedProcessor>().Use<FeedProcessor>();
            }));
    }

My FeedProcessor class has parameter constructor:

 public FeedProcessor(IFeedParserFactory ifeedParserFactory)
        {
            _ifeedParserFactory = ifeedParserFactory;
        }

This StructureMapControllerFactory :

    public class StructureMapControllerFactory : DefaultControllerFactory
    {
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
                if (requestContext.HttpContext.Request.Url != null)
                    throw new InvalidOperationException(string.Format("Page not found: {0}", requestContext.HttpContext.Request.Url.AbsoluteUri.ToString(CultureInfo.InvariantCulture)));
            return ObjectFactory.GetInstance(controllerType) as Controller;
        }
    }

and i get this error: Type 'Api.Controllers.FeedController' does not have a default constructor How can i config structure map for this scenario?


1 Answers

WebApi does not use the DefaultControlFactory to create instances of Api controllers. Instead, it uses DefaultHttpControllerSelector. However, unless you have a specific reason to create a controller factory, I would instead use the Dependency Resolver system built into MVC.

http://buchanan1966.tumblr.com/post/2192279804/mvc3-using-dependencyresolver-with-structuremap

http://marisks.net/2012/08/19/configuring-structuremap-in-aspnet-webapi/

like image 134
Erik Funkenbusch Avatar answered Jun 07 '26 09:06

Erik Funkenbusch