控制器没有默认的构造函数-Ninject,Owin,Web Api [英] Controller does not have a default constructor - Ninject, Owin, Web Api

查看:155
本文介绍了控制器没有默认的构造函数-Ninject,Owin,Web Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将MVC/WebApi应用程序迁移到使用Owin,但是在安装了所有组件并将所有配置从globals.asax移至Startup.cs之后,我得到了错误 Type'EventController'没有默认的构造函数.

I'm migrating an MVC/WebApi application to using Owin, but after installing all the components, and moving all configuration from the globals.asax to Startup.cs i'm getting the error Type 'EventController' does not have a default constructor.

看来我如何配置ninject不能正常工作.谁能发现错误?

It seems how i've got ninject configured isn't working correctly. Can anyone spot the error?

EventController

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类

Here's the startup.cs class

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


    }
}

推荐答案

我知道这是一个旧线程,并且已将解决方案标记为答案,但是这些解决方案都不适合我,却找到了另一个适合我的解决方案.将其发布给仍可能会遇到问题的其他人.

I know this is a old thread and have solutions marked as answers, but none of these worked for me and found another solution that worked for me. Posting it for others who still might be facing the problem.

我正在将 ASP.NET MVC 5与VS 2017和Ninject一起使用.我实现了该线程中提到的所有建议,但仍然遇到没有默认构造函数"运行时错误.我安装了Ninject.MVC5 nuget软件包,它可以正常工作.

I am using ASP.NET MVC 5 with VS 2017 and Ninject. I implemented all suggestions mentioned in this thread but I was still getting the 'does not have a default constructor' runtime error. I installed Ninject.MVC5 nuget package and it worked.

这篇关于控制器没有默认的构造函数-Ninject,Owin,Web Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆