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

编程入门 行业动态 更新时间:2024-10-08 14:49:31
本文介绍了使用Ninject确保控制器具有无参数的公共构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是眼前的问题:

通过URL调用我的CustomerController时,出现以下异常:

While calling my CustomerController through the URL, I get the following exception:

ExceptionMessage:

尝试创建类型的控制器时发生错误 'CustomerController'.确保控制器有一个 无参数的公共构造函数.

An error occurred when trying to create a controller of type 'CustomerController'. Make sure that the controller has a parameterless public constructor.

我正在使用以下网址:

  • localhost:55555/api/Customer/
  • localhost:55555/api/Customer/8
  • localhost:55555/api/Customer/
  • localhost:55555/api/Customer/8

请注意:在我将逻辑重构为业务类并实施依赖项注入之前,/api/Customer/调用已正常工作.

Please note: The /api/Customer/ call were working before I refactored the logic into a business class and implemented dependency injection.

我的研究表明,我没有在Ninject中正确注册我的界面和类,但是不确定我缺少什么步骤.

My research suggests that I am not registering my interface and class correctly with Ninject, but not sure what step I am missing.

研究链接:

  • 确保控制器具有一个无参数的公共构造函数错误
  • 确保控制器在Unity中有一个无参数的公共构造函数
  • Make sure that the controller has a parameterless public constructor error
  • Make sure that the controller has a parameterless public constructor in Unity

这是我的问题是什么导致此异常?我正在Ninject中注册我的接口/类,但似乎无法正确识别映射.有什么想法吗?

Here is my question What is causing this exception? I am registering my interface/class within Ninject, but it doesn't seem to recognize the mapping correctly. Any thoughts?

客户总监

public class CustomerController : ApiController { private readonly ICustomerBusiness _customerBusiness; public CustomerController(ICustomerBusiness customerBusiness) { _customerBusiness = customerBusiness; } // GET api/Customer [HttpGet] public IEnumerable<Customer> GetCustomers() { return _customerBusiness.GetCustomers(); } // GET api/Customer/Id [HttpGet] public IEnumerable<Customer> GetCustomersById(int customerId) { return _customerBusiness.GetCustomerById(customerId); } }

客户业务

public class CustomerBusiness : ICustomerBusiness { private readonly DatabaseContext _databaseContext = new DatabaseContext(); public IEnumerable<Customer> GetCustomers() { return _databaseContext.Customers; } public IQueryable<Customer> GetCustomerById(int customerId) { return _databaseContext.Customers.Where(c => c.CustomerId == customerId); } }

客户业务界面

public interface ICustomerBusiness { IQueryable<Customer> GetCustomerById(int customerId); IEnumerable<Customer> GetCustomers(); }

NinjectWebCommon

using System; using System.Web; using Microsoft.Web.Infrastructure.DynamicModuleHelper; using MyReservation.API; using MyReservation.API.Business; using Ninject; using Ninject.Web.Common; [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")] [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(NinjectWebCommon), "Stop")] namespace MyReservation.API { public static class NinjectWebCommon { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); /// <summary> /// Starts the application /// </summary> public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } /// <summary> /// Stops the application. /// </summary> public static void Stop() { bootstrapper.ShutDown(); } /// <summary> /// Creates the kernel that will manage your application. /// </summary> /// <returns>The created kernel.</returns> private static IKernel CreateKernel() { var kernel = new StandardKernel(); try { kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); RegisterServices(kernel); return kernel; } catch { kernel.Dispose(); throw; } } /// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { kernel.Bind<ICustomerBusiness>().To<CustomerBusiness>(); } } }

推荐答案

对于同一问题,我安装了nuget软件包

For the same problem I installed the nuget packages

  • 注入
  • ninject.webmon
  • ninject.webmon.webhost
  • ninject.web.webapi
  • ninject.web.webapi.webhost

  • ninject
  • ninject.webmon
  • ninject.webmon.webhost
  • ninject.web.webapi
  • ninject.web.webapi.webhost

工作

更多推荐

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

本文发布于:2023-11-17 11:06:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器具   有无   函数   参数   Ninject

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!