這裏一個無參數的公共構造函數是手頭上的問題:確保控制器具有使用Ninject
儘管打電話給我CustomerController
通過URL
,我得到以下異常:
ExceptionMessage :
嘗試創建類型爲 'CustomerController'的控制器時發生錯誤。確保控制器具有 無參數公共構造函數。
我使用以下網址:
請注意:的
/api/Customer/
電話進行工作前,我重構邏輯到業務類和實現依賴注入。
我的研究表明,我沒有正確註冊我的界面和類與Ninject
,但不知道我錯過了什麼步驟。
鏈接研究之:
- Make sure that the controller has a parameterless public constructor error
- Make sure that the controller has a parameterless public constructor in Unity
這裏是我的問題是什麼原因造成這個異常?我在
Ninject
內註冊了我的界面/類,但它似乎無法正確識別映射。有什麼想法嗎?
客戶控制器
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>();
}
}
}
你需要注入ICustomerBusiness您CustomerController。提示如何做到這一點,你可以在這裏找到:http://stackoverflow.com/questions/2227548/creating-an-instance-using-ninject-with-additional-parameters-in-the-constructor – MarkusE
可能重複的[ Ninject「沒有爲這個對象定義的無參數構造函數」](http://stackoverflow.com/questions/20127901/ninject-no-parameterless-constructor-defined-for-this-object) – BatteryBackupUnit
這個/類似的問題已經被問到很多的時代。您在「研究鏈接」下列出的兩個問題都是爲了統一。 Unity可能有不同的行爲,所以它可能不適用。你的問題是,你沒有正確設置你的項目使用ninject - 你的接收錯誤來自默認的依賴解析器 - ninject將永遠不會*發出該異常消息。 – BatteryBackupUnit