输入'Api.Controllers'。没有默认构造函数

时间:2013-04-20 19:01:33

标签: c# asp.net-mvc dependency-injection asp.net-web-api

我有一个使用依赖注入的控制器:

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

这是我的配置代码:

public static void Config()
    {

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

我的FeedProcessor类有参数构造函数:

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

这个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;
        }
    }

我收到此错误: 类型'Api.Controllers.FeedController'没有默认构造函数 如何为此方案配置结构图?

2 个答案:

答案 0 :(得分:3)

WebApi不使用DefaultControlFactory来创建Api控制器的实例。相反,它使用DefaultHttpControllerSelector。但是,除非您有特定的理由创建控制器工厂,否则我会使用内置于MVC中的Dependency Resolver系统。

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

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

答案 1 :(得分:0)

我最终得到了这个例外,因为我在我的应用程序的几个类中有周期性依赖。

相关问题