Ninject 无参数控制器的 WebApi 错误
Ninject WebApi error for parameterless controller
我已经阅读了我能找到的所有 Stack Overflow 问题,但无济于事。当我通过 NuGet 安装 Ninject 包时,NinjectWebCommon.cs class 没有安装,所以我自己编写了代码(如下)。我安装了 Ninject、Ninject.WebCommon、Ninject.Webcommon.Webhost、Ninject.WebApi、Ninject.WebApi.DependencyResolver。但是,我在我的控制器中收到一条众所周知的消息,即我必须有一个无参数控制器,当然我不想要它,因为我需要 DI。
这是我创建的 Ninject class:
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();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
//kernel.Load(Assembly.GetExecutingAssembly());
kernel.Bind<IUnitsSL>().To<UnitsSL>();
kernel.Bind<IForecastLogic>().To<ForecastLogic>();
kernel.Bind<IForecastRepositoryAsync>().To<ForecastRepositoryAsync>();
}
}
这是控制器
namespace ForecastApi.Controllers
{
public class ForecastController : ApiController
{
private IUnitsSL _serviceLayer { get; set; }
private readonly IForecastLogic _forecastLogic;
public ForecastController(IForecastLogic forecastLogic)
{
_forecastLogic = forecastLogic;
_serviceLayer = new UnitsSL();
}
[Route("projection")]
[HttpPost]
public async Task<IHttpActionResult> GetDemandForecast([FromBody]
ProjectionRequestModel model)
{
var retVal = await _forecastLogic.GetDemandForecastData(model);
return Ok(retVal);
}
}
当我通过 Postman 对此进行测试时,错误消息的确切措辞是:尝试创建 'ForecastController' 类型的控制器时发生错误。确保控制器具有无参数 public 构造函数。"
如果有帮助,我正在模拟业务调用的单元测试,它们正在使用 Ninject 库和最小起订量。似乎是构造函数失败了。
TIA
出于某种原因,完全卸载 Ninject 并重新安装(这是 3.3 版)解决了问题。如果其他人有类似问题,请将其留在这里。我最初安装了
- Ninject
- Ninject.Web.Common
- Ninject.Web.Common.WebHost
- Ninject.Web.WebApi
- Ninject.Web.WebApi.WebHost
当我卸载所有这些软件包,然后再次重新安装它们 v3.3 时,添加了 NinjectWebCommon.cs 文件并且一切正常。我只能假设当我滚动我自己的公共文件以弥补 Ninject 没有第一次安装它时缺少一些东西。
我已经阅读了我能找到的所有 Stack Overflow 问题,但无济于事。当我通过 NuGet 安装 Ninject 包时,NinjectWebCommon.cs class 没有安装,所以我自己编写了代码(如下)。我安装了 Ninject、Ninject.WebCommon、Ninject.Webcommon.Webhost、Ninject.WebApi、Ninject.WebApi.DependencyResolver。但是,我在我的控制器中收到一条众所周知的消息,即我必须有一个无参数控制器,当然我不想要它,因为我需要 DI。
这是我创建的 Ninject class:
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();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
//kernel.Load(Assembly.GetExecutingAssembly());
kernel.Bind<IUnitsSL>().To<UnitsSL>();
kernel.Bind<IForecastLogic>().To<ForecastLogic>();
kernel.Bind<IForecastRepositoryAsync>().To<ForecastRepositoryAsync>();
}
}
这是控制器
namespace ForecastApi.Controllers
{
public class ForecastController : ApiController
{
private IUnitsSL _serviceLayer { get; set; }
private readonly IForecastLogic _forecastLogic;
public ForecastController(IForecastLogic forecastLogic)
{
_forecastLogic = forecastLogic;
_serviceLayer = new UnitsSL();
}
[Route("projection")]
[HttpPost]
public async Task<IHttpActionResult> GetDemandForecast([FromBody]
ProjectionRequestModel model)
{
var retVal = await _forecastLogic.GetDemandForecastData(model);
return Ok(retVal);
}
}
当我通过 Postman 对此进行测试时,错误消息的确切措辞是:尝试创建 'ForecastController' 类型的控制器时发生错误。确保控制器具有无参数 public 构造函数。"
如果有帮助,我正在模拟业务调用的单元测试,它们正在使用 Ninject 库和最小起订量。似乎是构造函数失败了。
TIA
出于某种原因,完全卸载 Ninject 并重新安装(这是 3.3 版)解决了问题。如果其他人有类似问题,请将其留在这里。我最初安装了
- Ninject
- Ninject.Web.Common
- Ninject.Web.Common.WebHost
- Ninject.Web.WebApi
- Ninject.Web.WebApi.WebHost
当我卸载所有这些软件包,然后再次重新安装它们 v3.3 时,添加了 NinjectWebCommon.cs 文件并且一切正常。我只能假设当我滚动我自己的公共文件以弥补 Ninject 没有第一次安装它时缺少一些东西。