当我从Visual Studio运行我的WebAPI项目时,它运行得很好。但是,当我发布到文件系统并将文件复制到我的IIS文件夹并运行它时,它失败并显示错误:An error occurred when trying to create a controller of type 'DepartmentsController'. Make sure that the controller has a parameterless public constructor.
关于SO的其他一些答案就像这样:MVC5, Web API 2 and and Ninject 说你需要安装Ninject.Web.WebApi.WebHost,我已经完成了。
我的所有绑定都已设置,所有内容都在NinjectWebCommon中初始化。当我在本地调试时,正在调用NinjectWebCommon.Start()。我不知道是什么调用它,因为我无法在应用程序的任何地方找到对NinjectWebCommon.Start()的引用。为什么这会在本地调试中工作但在IIS中失败?
using System.Web.Http;
using MyApp.WebAPI.Infrastructure;
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MyApp.WebAPI.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(MyApp.WebAPI.App_Start.NinjectWebCommon), "Stop")]
namespace MyApp.WebAPI.App_Start {
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
public static class NinjectWebCommon {
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
public static void Start() {
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
IKernel container = null;
bootstrapper.Initialize(() => {
container = CreateKernel();
return container;
});
NinjectDependencyResolver resolver = new NinjectDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
}
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) {
NinjectConfigurator containerConfigurator = new NinjectConfigurator();
containerConfigurator.Configure(kernel);
}
}
}
这是我绑定的地方:
using MyApp.DataAccess.Entities;
using MyApp.Domain.Abstract;
using MyApp.DataAccess.Repositories.Concrete;
using Ninject;
using Ninject.Web.Common;
namespace MyApp.WebAPI.App_Start {
public class NinjectConfigurator {
public void Configure(IKernel container) {
AddBindings(container);
}
private void AddBindings(IKernel container) {
container.Bind<MyAppContext>().ToSelf().InRequestScope();
container.Bind<IUnitOfWork<MyAppContext>>().To<UnitOfWork<MyAppContext>>();
container.Bind<IUserRepository>().To<UserRepository>();
container.Bind<ITeamRepository>().To<TeamRepository>();
container.Bind<IDepartmentRepository>().To<DepartmentRepository>();
container.Bind<IDivisionRepository>().To<DivisionRepository>();
}
}
}
这是控制器:
using System;
using System.Collections.Generic;
using System.Web.Http;
using MyApp.DataAccess.Entities;
using MyApp.Domain.Abstract;
using Department = MyApp.Domain.Models.Department;
namespace MyApp.WebAPI.Controllers
{
public class DepartmentsController : ApiController {
private IUnitOfWork<MyAppContext> _unitOfWork;
public DepartmentsController(IUnitOfWork<MyAppContext> unitOfWork) {
_unitOfWork = unitOfWork;
}
[HttpGet]
public IHttpActionResult Get() {
IEnumerable<Department> deptsList = _unitOfWork.Departments.GetAll();
return Ok(deptsList);
}
[HttpGet]
public IHttpActionResult Get(int id) {
Department dept = _unitOfWork.Departments.Get(id);
return Ok(dept);
}
[HttpGet]
[Route("api/departments/division/{divisionId:int}")]
public IHttpActionResult GetDepartmentsByDivision(int divisionId) {
IEnumerable<Department> departmentsInDivision = _unitOfWork.Departments.GetDepartmentsByDivisionId(divisionId);
return Ok(departmentsInDivision);
}
[HttpPost]
public IHttpActionResult Post(Department department) {
_unitOfWork.Departments.Add(department);
//Make sure departmentId is updated from DB before passing back
return Created("api/departments/" + department.DepartmentId, department);
}
[HttpPut]
public IHttpActionResult Put(Department department) {
_unitOfWork.Departments.Update(department);
return Ok(department);
}
}
}
答案 0 :(得分:0)
当我使用NuGet将EntityFramework安装到我的WebAPI项目时,一切都开始工作了。这没有任何意义,完全违背了DI / Ninject和我的数据访问层的目的,但我会在一个好的设计中采用一种糟糕的设计。那不起作用。