结构图 - 确保控制器具有无参数的公共构造函数

时间:2016-06-03 01:59:21

标签: c# dependency-injection structuremap

自从VS2013将我的WebApi项目迁移到VS2015后,我无法使我的依赖注入工作。我正在使用StructureMap。我收到包含此异常消息的错误:“尝试创建类型为'SearchListController'的控制器时发生错误。确保控制器具有无参数的公共构造函数。”这曾经工作过。谁能告诉我我做错了什么?如果我没有正确地写这个问题,请原谅我。我想这是我在这里发布的第一个问题。

namespace SearchWebAPI.DependencyResolution
    {
        using StructureMap;

        public static class IoC
        {
            public static IContainer Initialize()
            {
                return new Container(c =>
                {
                    c.AddRegistry<DefaultRegistry>();
                });
             }
        }
    }

namespace SearchWebAPI.DependencyResolution {
    using StructureMap.Configuration.DSL;
    using StructureMap.Graph;
    using SearchWebAPI.Models;

    public class DefaultRegistry : Registry {
        #region Constructors and Destructors

        public DefaultRegistry() {
            Scan(
                scan => {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                    scan.With(new ControllerConvention());
                });
            For<ISearchListRepository>().Use<SearchListRepository>();
        }

        #endregion
    }
}

2 个答案:

答案 0 :(得分:2)

您看到的错误(尝试创建“SearchListController”类型的控制器时发生错误)是StructureMap无法解析控制器中的依赖关系的结果,通常是耦合的。

看了你的代码后,很难看出问题出在哪里。您如何使用ASP.NET应用程序配置StructureMap? (通过您自己的控制器工厂或通过MVC的DependencyResolver?),您能提供更多代码吗?

此外,使用StructureMaps的AssertConfigurationIsValid();是调试问题的好方法。你可以这样称呼它:

var container = new Container();
container.AssertConfigurationIsValid();

答案 1 :(得分:2)

我现在有这个工作。这是我的代码:

namespace SearchWebAPI.DependencyResolution {
    using StructureMap;

    public static class IoC {
        public static IContainer Initialize() {
            return new Container(c => c.AddRegistry<DefaultRegistry>());
        }
    }
}

namespace SearchWebAPI.DependencyResolution
{
    using StructureMap.Configuration.DSL;
    using StructureMap.Graph;
    using SearchWebAPI.Models;

    public class DefaultRegistry : Registry
    {
        #region Constructors and Destructors

        public DefaultRegistry()
        {
            Scan(
                scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                    scan.With(new ControllerConvention());
                });
        }

        #endregion
    }
}

namespace SearchWebAPI
{
    using SearchWebAPI.DependencyResolution;
    using StructureMap;
    using System.Web.Http;
    using System.Web.Mvc;

    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            App_Start.StructuremapMvc.Start();

            //StructureMap Container
            IContainer container = IoC.Initialize();

            //Register for MVC
            DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));

            //Register for Web API
            GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);
        }
    }
}

两行代码解决了我的问题:

//Register for MVC
DependencyResolver.SetResolver(new StructureMapDependencyResolve(container));

//Register for Web API
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapDependencyResolver(container);