0
我正在將MVC/WebApi應用程序遷移到使用Owin,但安裝完所有組件並將所有配置從globals.asax移動到Startup.cs我收到錯誤Type 'EventController' does not have a default constructor
。控制器沒有默認的構造函數 - Ninject,Owin,Web Api
看來我的ninject配置不正常。任何人都可以發現錯誤?
EventController
public class EventController : BaseApiController
{
private readonly IAttendeeService attendeeService;
private readonly IEventService eventService;
private readonly IExcelService excelService;
public EventController(IEventService eventService, IAttendeeService attendeeService, IExcelService excelService)
{
this.attendeeService = attendeeService;
this.eventService = eventService;
this.excelService = excelService;
}
}
這裏的startup.cs類
using System.Linq;
using System.Net.Http.Formatting;
using System.Reflection;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Filanthropy.Core;
using Filanthropy.Model.Models;
using Filanthropy.Services;
using Filanthropy.Web;
using Filanthropy.Web.App_Start;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Newtonsoft.Json.Serialization;
using Ninject;
using Ninject.Web.Common;
using Ninject.Web.Common.OwinHost;
using Ninject.Web.WebApi.OwinHost;
using Owin;
[assembly: OwinStartup(typeof(Startup))]
namespace Filanthropy.Web
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration httpConfig = new HttpConfiguration();
ConfigureOAuthTokenGeneration(app);
ConfigureWebApi(httpConfig);
app.UseCors(CorsOptions.AllowAll);
app.UseWebApi(httpConfig);
app.MapSignalR();
app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(httpConfig);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AutoMapperConfig.Configure();
}
private void ConfigureOAuthTokenGeneration(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(FilanthropyContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Plugin the OAuth bearer JSON Web Token tokens generation and Consumption will be here
}
private void ConfigureWebApi(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
private static StandardKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
RegisterServices(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.Bind<IDbContext>().To<FilanthropyContext>().InRequestScope();
kernel.Bind<IEventService>().To<EventService>().InRequestScope();
kernel.Bind<IAttendeeService>().To<AttendeeService>().InRequestScope();
kernel.Bind<IProjectService>().To<ProjectService>().InRequestScope();
kernel.Bind<IPaymentService>().To<PaymentService>().InRequestScope();
kernel.Bind<IPledgeService>().To<PledgeService>().InRequestScope();
kernel.Bind<IExcelService>().To<ExcelService>();
kernel.Bind<IUserStore<ApplicationUser>>()
.To<UserStore<ApplicationUser>>()
.WithConstructorArgument("context", context => kernel.Get<FilanthropyContext>());
}
}
}
看一看[本類似的問題](http://stackoverflow.com/questions/17462175/mvc-4-web-api-controller-does-not-have-a-default-constructor ) – Jonesopolis
你有沒有試過在app.UseWebApi(httpConfig)之前放'app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(httpConfig);''? – Pinpoint
這會導致循環依賴性錯誤@Pinpoint – MrBliz