2015-06-20 66 views
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>()); 
     }  


    } 
} 
+0

看一看[本類似的問題](http://stackoverflow.com/questions/17462175/mvc-4-web-api-controller-does-not-have-a-default-constructor ) – Jonesopolis

+0

你有沒有試過在app.UseWebApi(httpConfig)之前放'app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(httpConfig);''? – Pinpoint

+0

這會導致循環依賴性錯誤@Pinpoint – MrBliz

回答

0

你需要告訴Ninject如何正確地解決網絡API的依賴。編輯:NinjectWebCommon.cs並更新CreateKernel()方法以包括:GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);

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

     //Note: Add the line below: 
     GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel); 

     return kernel; 
    } 
    catch 
    { 
     kernel.Dispose(); 
     throw; 
    } 
} 
相關問題