使用Ninject确保控制器具有无参数的公共构造函数 [英] Make sure that the controller has a parameterless public constructor using 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.
我正在使用以下网址:
- http://localhost:55555/api/Customer/
- http://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.
研究链接:
- 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.web.common
- ninject.web.common.webhost
- ninject.web.webapi
-
ninject.web.webapi.webhost
- ninject
- ninject.web.common
- ninject.web.common.webhost
- ninject.web.webapi
ninject.web.webapi.webhost
工作
这篇关于使用Ninject确保控制器具有无参数的公共构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!