2015-06-20 81 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; 
    } 
} 
相关问题