gpt4 book ai didi

c# - 无参数 Controller 的 Ninject WebApi 错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:59:23 25 4
gpt4 key购买 nike

我已经阅读了我能找到的所有 Stack Overflow 问题,但无济于事。当我通过 NuGet 安装 Ninject 包时,没有安装 NinjectWebCommon.cs 类,所以我自己编写了代码(如下)。我安装了 Ninject、Ninject.WebCommon、Ninject.Webcommon.Webhost、Ninject.WebApi、Ninject.WebApi.DependencyResolver。但是,我在我的 Controller 中收到一条众所周知的消息,即我必须有一个无参数 Controller ,当然我不想要它,因为我需要 DI。

这是我创建的 Ninject 类:

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>();
}
}

这是 Controller

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”类型的 Controller 时发生错误。确保 Controller 有一个无参数的公共(public)构造函数。”

如果有帮助,我正在为业务调用模拟我的单元测试,它们正在使用 Ninject 库和 Moq。似乎是构造函数失败了。

TIA

最佳答案

无论出于何种原因,完全卸载 Ninject 并重新安装它(这是 3.3 版)解决了问题。如果其他人有类似问题,请将其留在这里。我最初安装

  • 注入(inject)
  • Ninject.Web.Common
  • Ninject.Web.Common.WebHost
  • Ninject.Web.WebApi
  • Ninject.Web.WebApi.WebHost

当我卸载所有这些软件包,然后再次重新安装它们 v3.3 时,添加了 NinjectWebCommon.cs 文件并且一切正常。我只能假设当我滚动自己的公共(public)文件以弥补 Ninject 没有第一次安装它时缺少某些东西。

关于c# - 无参数 Controller 的 Ninject WebApi 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46980329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com