尝试创建类型为' EmployeeController'的控制器时发生错误。确保控制器具有无参数的公共构造函数

时间:2017-07-18 08:00:40

标签: c# .net asp.net-mvc asp.net-web-api unity-container

为什么我在员工控制器上收到此错误,其余的工作正常 这是我的员工控制器

public class EmployeeController : ApiController
    {
        #region Call service
        private readonly IEmployeeServices _employeeServices;
        public EmployeeController(IEmployeeServices employeeServices)
        {
            _employeeServices = employeeServices;
        }

        readonly IEmployeeServices employeeServices = new EmployeeServices();

        public EmployeeController():base()
        {
            _employeeServices = employeeServices;
        }
}

这是我完美的工作产品控制器

public class ProductController : ApiController
    {
        #region Call service

        private readonly IProductServices _productServices;

        public ProductController(IProductServices productServices)
        {
            _productServices = productServices;
        }

        readonly IProductServices productServices = new ProductServices();

        public ProductController()
        {
            _productServices = productServices;
        }
}

这是堆栈跟踪

  

发生错误。尝试创建' EmployeeController'类型的控制器时发生错误。确保控制器在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,Type controllerType)
中具有无参数公共构造函数.System.InvalidOperationException。在System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)
在System.Web.Http.Dispatcher.HttpControllerDispatcher。< SendAsync> d__1.MoveNext()发生错误。依赖关系的解析失败,输入=" TheWork.Controllers.EmployeeController",name =&# 34;(无)"
在解决时发生异常。
异常是:InvalidOperationException - 当前类型BusinessServices.IEmployeeServices是一个接口,无法构造。你错过了类型映射吗?
 -----------------------------------------------&#13 ;在例外时,容器是:
 
解决TheWork.Controllers.EmployeeController,(无)
解决参数" employeeServices"构造函数TheWork.Controllers.EmployeeController(BusinessServices.IEmployeeServices employeeServices)
解决BusinessServices.IEmployeeServices,(无)
 Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t,Object existing,String name,IEnumerable 1 resolverOverrides)
 at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name, ResolverOverride[] resolverOverrides)
 at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve(IUnityContainer container, Type t, ResolverOverride[] overrides)
 at Unity.WebApi.UnityDependencyScope.GetService(Type serviceType)
 at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func 1& activator)
 Microsoft.Practices.Unity.ResolutionFailedException。在System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,类型controllerType)发生错误。当前类型BusinessServices.IEmployeeServices是一个接口,无法构造。您是否缺少类型映射?Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForAttemptingToConstructInterface(IBuilderContext context)
中的System.InvalidOperationException在lambda_method(Closure,IBuilderContext)
在Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext。<> c__DisplayClass1。< GetBuildMethod> b__0(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey)
在Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context)
在lambda_method(Closure,IBuilderContext)
在Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext。<> c__DisplayClass1。< GetBuildMethod> b__0(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
在Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
在Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t,Object existing,String name,IEnumerable`1 resolverOverrides)

更新

这是Unity配置

   public static class UnityConfig
    {
        public static void RegisterComponents()
        {
            var container = new UnityContainer();
            System.Web.Mvc.DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

            RegisterTypes(container);
        }

        public static void RegisterTypes(IUnityContainer container)
        {
            ComponentLoader.LoadContainer(container, ".\\bin", "TheWork.dll");
            ComponentLoader.LoadContainer(container, ".\\bin", "BusinessServices.dll");
            ComponentLoader.LoadContainer(container, ".\\bin", "DataModel.dll");
        }
}

3 个答案:

答案 0 :(得分:9)

埋藏在堆栈跟踪中是问题的根本原因:

  

InvalidOperationException - 当前类型    BusinessServices.IEmployeeServices,是一个界面,不能   的构造。你错过了类型映射吗?    - - - - - - - - - - - - - - - - - - - - - - - - 当时例外,容器是:解决   TheWork.Controllers.EmployeeController,(无)解析参数   " employeeServices"构造函数   TheWork.Controllers.EmployeeController(BusinessServices.IEmployeeServices   employeeServices)解析BusinessServices.IEmployeeServices,(无)   Microsoft.Practices.Unity.ResolutionFailedException at   Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t,Object   existing,String name,IEnumerable1 resolverOverrides)

问题是EmployeeController需要IEmployeeServices的实例,但Unity不知道要实例化的具体类型。看起来实现类应该通过调用ComponentLoader.LoadContainer(container, ".\\bin", "BusinessServices.dll");来注册,但由于某种原因它没有被注册。它可能是该代码中的错误,或者可能是BusinessServices.dll已过期且不包含IEmployeeServices定义。

如果没有看到所有代码和运行时依赖项(因为动态加载/注册类型),就很难说IEmployeeServices未注册的原因。

答案 1 :(得分:0)

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

public class EmployeeController : ApiController
    {
        #region Call service
        private readonly IEmployeeServices _employeeServices;
        public EmployeeController(IEmployeeServices employeeServices)
        {
            _employeeServices = employeeServices;
        }

        readonly IEmployeeServices employeeServices = new EmployeeServices();

        public EmployeeController()
        {
            _employeeServices = employeeServices;
        }
}

答案 2 :(得分:0)

尝试这种方式:)

        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IEmployeeServices >().To<EmployeeServices >().InRequestScope();
        }