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

时间:2016-08-02 20:22:39

标签: c# asp.net-web-api dependency-injection ninject inversion-of-control

是的,我尝试使用Stackoverflow问题/答案中的其他建议进行修复。

  1. 奇怪的是,我以为我已经修好了......
  2. 我读过关于web api和mvc解析器与ninject的不同之处,所以我认为我在一周前修好了。
  3. 在删除现有软件包之后,我最终得到了nuget的所有软件包,所以也许Nuget搞砸了什么?
  4. 这是我的错误

        {
          "Message": "An error has occurred.",
          "ExceptionMessage": "An error occurred when trying to create a controller of type 'AssignmentController'. Make sure that the controller has a parameterless public constructor.",
          "ExceptionType": "System.InvalidOperationException",
          "StackTrace": "   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
          "InnerException": {
            "Message": "An error has occurred.",
            "ExceptionMessage": "Error activating IGenericRepository{InternInterest}\r\nNo matching bindings are available, and the type is not self-bindable.\r\nActivation path:\r\n  3) Injection of dependency IGenericRepository{InternInterest} into parameter internInterestRepository of constructor of type UnitOfWork\r\n  2) Injection of dependency IUnitOfWork into parameter unitOfWork of constructor of type AssignmentController\r\n  1) Request for AssignmentController\r\n\r\nSuggestions:\r\n  1) Ensure that you have defined a binding for IGenericRepository{InternInterest}.\r\n  2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.\r\n  3) Ensure you have not accidentally created more than one kernel.\r\n  4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.\r\n  5) If you are using automatic module loading, ensure the search path and filters are correct.\r\n",
            "ExceptionType": "Ninject.ActivationException",
            "StackTrace": "   at Ninject.KernelBase.Resolve(IRequest request)\r\n   at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)\r\n   at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)\r\n   at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)\r\n   at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)\r\n   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()\r\n   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)\r\n   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)\r\n   at Ninject.Activation.Providers.StandardProvider.Create(IContext context)\r\n   at Ninject.Activation.Context.ResolveInternal(Object scope)\r\n   at Ninject.Activation.Context.Resolve()\r\n   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)\r\n   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)\r\n   at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)\r\n   at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)\r\n   at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)\r\n   at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)\r\n   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()\r\n   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)\r\n   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)\r\n   at Ninject.Activation.Providers.StandardProvider.Create(IContext context)\r\n   at Ninject.Activation.Context.ResolveInternal(Object scope)\r\n   at Ninject.Activation.Context.Resolve()\r\n   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)\r\n   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)\r\n   at Ninject.Web.WebApi.NinjectDependencyScope.GetService(Type serviceType)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
          }
        }
    

    网址

    http://localhost/PublicHealth.Intern.Business.WebApi/api/assignment/GetAssignments

    Ninject的一部分

    private static void RegisterServices(IKernel kernel)
    {
       kernel.Load(Assembly.GetExecutingAssembly());
    }
    

    web api控制器

    public class AssignmentController : ApiController
    {
        private readonly IUnitOfWork _UnitOfWork;
    
        public AssignmentController(IUnitOfWork unitOfWork)
        {
            _UnitOfWork = unitOfWork;
        }
    
        public List<Assignment> GetAssignments()
        {
            var result = _UnitOfWork.AssignmentsRepository.Get();
            return result;
        }
    
        //......
      }
    

0 个答案:

没有答案