Ninject Web Api“确保控制器具有无参数的公共构造函数。”

我已经使用ninject差不多2年但现在使用它我的ASP.NET MVC / WebAPI项目我收到此消息和stackoverflow上的先前线程与各种建议并没有解决我的问题。 我有以下nuget包:Ninject MVC3 Ninject Integration for WebApi 2。

我已经尝试解决问题的时间越来越长,我也会非常感谢我能得到的任何帮助和建议! (如果有人想仔细看看,我很乐意把解决方案放在Github上)

这是我使用的类:

public class CmsContext : DbContext { public CmsContext() : base("CMS_POC") { } public DbSet Activities { get; set; } public DbSet Systems { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove(); } } public interface IRepository where T : IEntityBase { IEnumerable GetAll { get; } void Add(T entity); void Delete(T entity); void Update(T entity); T GetById(int Id); } public class SystemRepository : IRepository { private CmsContext _systemContext; public SystemRepository(CmsContext systemContext) { _systemContext = systemContext; } public IEnumerable GetAll { get { return _systemContext.Systems; } } public void Add(Entities.System entity) { _systemContext.Systems.Add(entity); _systemContext.SaveChanges(); } public void Delete(Entities.System entity) { _systemContext.Systems.Remove(entity); _systemContext.SaveChanges(); } public void Update(Entities.System entity) { var system = _systemContext.Systems.SingleOrDefault(s => s.System_Id == entity.System_Id); system = entity; _systemContext.SaveChanges(); } public Entities.System GetById(int Id) { return _systemContext.Systems.SingleOrDefault(s => s.System_Id == Id); 

}

 public class ActivityRepository : IRepository { private CmsContext _activityContext; public ActivityRepository(CmsContext activityContext) { _activityContext = activityContext; } public IEnumerable GetAll { get { return _activityContext.Activities; } } public void Add(Activity entity) { _activityContext.Activities.Add(entity); _activityContext.SaveChanges(); } public void Delete(Activity entity) { _activityContext.Activities.Remove(entity); _activityContext.SaveChanges(); } public void Update(Activity entity) { var activity = _activityContext.Activities.SingleOrDefault(a => a.Activity_Id == entity.Activity_Id); activity = entity; _activityContext.SaveChanges(); } public Activity GetById(int Id) { return _activityContext.Activities.SingleOrDefault(a => a.Activity_Id == Id); } } public interface IActivityService { IReadOnlyList GetActivities(); Activity GetSingleActivity(int id); Activity CreateActivity(string activityName, DateTime startDate, DateTime endDate, int systemId); Activity UpdateActivity(int activityId, string activityName, DateTime startDate, DateTime endDate); void DeleteActivity(int activityId); } public interface ISystemService { IReadOnlyList GetSystems(); CMS.Entities.System GetSingleSystem(int id); CMS.Entities.System CreateSystem(string systemName); CMS.Entities.System UpdateSystem(int systemId, string systemName); void DeleteSystem(int systemId); } public class ActivityService : IActivityService { private IRepository _activityRepository; private IRepository _systemRepository; public ActivityService(IRepository activityRepository, IRepository systemRepository) { _activityRepository = activityRepository; _systemRepository = systemRepository; } } 

这只是服务类的一小部分,但我想展示我如何在服务中使用DI。

最后是ninject的配置:

 public static class NinjectWebCommon { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); ///  /// Starts the application ///  public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } ///  /// Stops the application. ///  public static void Stop() { bootstrapper.ShutDown(); } ///  /// Creates the kernel that will manage your application. ///  /// The created kernel. private static IKernel CreateKernel() { var kernel = new StandardKernel(); try { kernel.Bind<Func>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind().To(); RegisterServices(kernel); return kernel; } catch { kernel.Dispose(); throw; } } ///  /// Load your modules or register your services here! ///  /// The kernel. private static void RegisterServices(IKernel kernel) { kernel.Bind().To(); kernel.Bind().To(); kernel.Bind<IRepository>().To(); kernel.Bind<IRepository>().To(); } } 

编辑*忘了我的控制器

 public class ActivitiesController : ApiController { private IActivityService _activityService; public ActivitiesController(IActivityService activityService) { _activityService = activityService; } } 

所以我从来没有真正使用过ninject,但我花了很多时间在WebApi中使用结构图(甚至已经完成了关于WebApi如何创建控制器的工作 )并且我很确定我知道最新情况。

看起来你从来没有用WebApi注册你的内核。 WebApi通过IControllerActivator创建控制器(您可以替换它,但可能不想,因为它会为您执行大量缓存和操作),而后者又会调用DependencyResolver来创建控制器。 如果你还没有注册自己的解析器,它只会使用Activator.CreateInstance()来抛出你看到的错误。

您需要使用库中的IDependencyResolver(例如这个 )或实现您自己的IDependencyResolver和IDependencyScope(可以在此处找到StructureMap示例)。 实现后,您可以将其注册到Web API,如下所示:

 GlobalConfiguration.Configuration.DependencyResolver = myResolverImpl; 

如果您对此有任何疑问或者这不起作用请问,这是一个我正在努力确保我理解得很好的领域,所以我很乐意填补我的知识。