在控制器中使用带有路由属性的 Ninject 和 Web API 2 时出错
Error using Ninject and Web API 2 with route attribute in controllers
我有一个实现 Ninject 的 Web API 2 项目。如果我的控制器不使用 Route 属性,它工作正常,但是,如果我使用它们,应用程序 returns 出现以下异常:"An error occurred when trying to create a controller of type 'AccountsController'. Make sure that the controller has a parameterless public constructor."
工作正常
public IHttpActionResult get()
{
var entities = EntityService.getAll();
return Ok(entities);
}
不工作
[Route("user")]
public IHttpActionResult get()
{
var entities = EntityService.getAll();
return Ok(entities);
}
我安装了以下软件包
Ninject version="3.2.0.0"
Ninject.Extensions.ContextPreservation version="3.2.0.0"
Ninject.Extensions.NamedScope version="3.2.0.0"
Ninject.Web.Common version="3.2.0.0"
Ninject.Web.Common.OwinHost version="3.2.3.0"
Ninject.Web.Common.WebHost version="3.2.3.0"
Ninject.Web.WebApi version="3.2.4.0"
Ninject.Web.WebApi.OwinHost version="3.2.4.0"
Ninject.Web.WebApi.WebHost
我的 NintextWebCommon class 是
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(ProperdiAPI.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(ProperdiAPI.App_Start.NinjectWebCommon), "Stop")]
namespace ProperdiAPI.App_Start
{
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using System;
using System.Web;
using System.Web.Http;
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);
}
public static void Stop()
{
bootstrapper.ShutDown();
}
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;
}
}
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IEntityService>().To<EntityService>().InRequestScope();
}
}
}
BaseApiController
public class BaseApiController : ApiController
{
private IEntityService entitysService;
public BaseApiController(IEntityService entityService)
{
this.entityService = entityService;
}
protected IEntityService EntitysService
{
get
{
return this.entityService;
}
}
protected IHttpActionResult GetErrorResult(IdentityResult result)
{
if (result == null)
{
return InternalServerError();
}
if (!result.Succeeded)
{
if (result.Errors != null)
{
foreach (string error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
if (ModelState.IsValid)
{
return BadRequest();
}
return BadRequest(ModelState);
}
return null;
}
}
}
控制器
[RoutePrefix("api/accounts")]
public class AccountsController : BaseApiController
{
public AccountsController(IEntityService entityService)
:base(entityService)
{
}
[HttpGet]
//[Route("user")]
public IHttpActionResult get()
{
var entities = EntityService .getAll();
return Ok(entities);
}
}
我尝试了很多方法,例如构建自定义解析器和作用域、安装旧的 ninject 版本等等,但没有任何效果。
非常感谢!
安装插件 Ninject.WebApi.DependencyResolver
并在调用 RegisterServices(kernel);
后添加此代码:
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);
我有一个实现 Ninject 的 Web API 2 项目。如果我的控制器不使用 Route 属性,它工作正常,但是,如果我使用它们,应用程序 returns 出现以下异常:"An error occurred when trying to create a controller of type 'AccountsController'. Make sure that the controller has a parameterless public constructor."
工作正常
public IHttpActionResult get()
{
var entities = EntityService.getAll();
return Ok(entities);
}
不工作
[Route("user")]
public IHttpActionResult get()
{
var entities = EntityService.getAll();
return Ok(entities);
}
我安装了以下软件包
Ninject version="3.2.0.0"
Ninject.Extensions.ContextPreservation version="3.2.0.0"
Ninject.Extensions.NamedScope version="3.2.0.0"
Ninject.Web.Common version="3.2.0.0"
Ninject.Web.Common.OwinHost version="3.2.3.0"
Ninject.Web.Common.WebHost version="3.2.3.0"
Ninject.Web.WebApi version="3.2.4.0"
Ninject.Web.WebApi.OwinHost version="3.2.4.0"
Ninject.Web.WebApi.WebHost
我的 NintextWebCommon class 是
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(ProperdiAPI.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(ProperdiAPI.App_Start.NinjectWebCommon), "Stop")]
namespace ProperdiAPI.App_Start
{
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using System;
using System.Web;
using System.Web.Http;
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);
}
public static void Stop()
{
bootstrapper.ShutDown();
}
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;
}
}
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IEntityService>().To<EntityService>().InRequestScope();
}
}
}
BaseApiController
public class BaseApiController : ApiController
{
private IEntityService entitysService;
public BaseApiController(IEntityService entityService)
{
this.entityService = entityService;
}
protected IEntityService EntitysService
{
get
{
return this.entityService;
}
}
protected IHttpActionResult GetErrorResult(IdentityResult result)
{
if (result == null)
{
return InternalServerError();
}
if (!result.Succeeded)
{
if (result.Errors != null)
{
foreach (string error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
if (ModelState.IsValid)
{
return BadRequest();
}
return BadRequest(ModelState);
}
return null;
}
}
}
控制器
[RoutePrefix("api/accounts")]
public class AccountsController : BaseApiController
{
public AccountsController(IEntityService entityService)
:base(entityService)
{
}
[HttpGet]
//[Route("user")]
public IHttpActionResult get()
{
var entities = EntityService .getAll();
return Ok(entities);
}
}
我尝试了很多方法,例如构建自定义解析器和作用域、安装旧的 ninject 版本等等,但没有任何效果。
非常感谢!
安装插件 Ninject.WebApi.DependencyResolver
并在调用 RegisterServices(kernel);
后添加此代码:
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);