ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object

前端 未结 17 1441
猫巷女王i
猫巷女王i 2020-11-30 01:41

UPDATE - Please look at my answer for a link and explanation of the solution to this problem

Before we start, I know this is a very common quest

相关标签:
17条回答
  • 2020-11-30 02:30

    Shortcut for all this: Download one of the following Nuget Pacakges:

    • Ninject.MVC3
    • Ninject.MVC5

    That will take care of the binding that you need.

    0 讨论(0)
  • 2020-11-30 02:31

    The Problem seems to occur when one uses Visual Studio 2012/2013 with ASP.NET 4.0/4.5. The version of System.Web.Mvc was set to 4.0.0.0 in the Web.config file. It did not work.

    My solution was to

    1. delete NinjectWebCommon.cs
    2. copy Dere Jone's NinjectControllerFactory class into the project:

      public class NinjectControllerFactory : DefaultControllerFactory
      {
         private IKernel ninjectKernel;
         public NinjectControllerFactory(IKernel kernel)
         {
            ninjectKernel = kernel;
         }
      
         protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
         {
            return (controllerType == null) ? null : (IController) ninjectKernel.Get(controllerType);
         }
      }
      
    3. change the content of Global.asax to:

      public class MvcApplication : NinjectHttpApplication
      {
         protected override IKernel CreateKernel()
         {
             IKernel kernel = new StandardKernel();
      
             // kernel.Load(Assembly.GetExecutingAssembly());
      
             // kernel.Bind<ISomeClass>().To<SomeClass>();
      
             return kernel;
         }
      
         protected override void OnApplicationStarted()
         {
             AreaRegistration.RegisterAllAreas();
      
             WebApiConfig.Register(GlobalConfiguration.Configuration);
             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
             RouteConfig.RegisterRoutes(RouteTable.Routes);
             BundleConfig.RegisterBundles(BundleTable.Bundles);
      
             ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(Kernel));
         }
      }
      

      after that your injections should work.

    0 讨论(0)
  • 2020-11-30 02:31

    I was getting this issue but only on Web Service calls. Found my solution on this answer https://stackoverflow.com/a/12379938/129580 below is the sample code provided

    public class Service : WebService
    {
      public IContextProvider ContextProvider {get;set;}
    
      public Service()
      {
        ContextProvider = DependencyResolver.Current.GetService<IContextProvider>();
      }
    }
    

    which I then ended up turning into, hope this helps...

    /// <summary>
    /// Summary description for PartMarking
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class PartMarking : System.Web.Services.WebService
    {
        private readonly IUnitOfWork _unitOfWork;
    
        public PartMarking()
        {
            _unitOfWork = DependencyResolver.Current.GetService<IUnitOfWork>();
        }
    
        [WebMethod]
        public DataSet GetParentConfigurations(string engineModel, string componentCode)
        {
            var results = _unitOfWork.PartMarking.GetParentSicMarkings(engineModel, componentCode);
            return results;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 02:34

    I tried all these solutions, but none worked.

    This is how I resolved it: Deleted all my temp asp.net files. After I'd done this, I got new config errors. I fixed them. The last, and real error came out: new Bootstrapper() was trying to load an older version of a ninject dll. Removed all ninject packages. Installed ninject nuget packages one by one, make triply sure that the correct version is installed.

    0 讨论(0)
  • 2020-11-30 02:37

    I tried to set up Ninject with MVC 5. Finally i had to use solution with controller factory first, and solution with binding redirect then. After that all works as expected. Used Local IIS server.

    0 讨论(0)
提交回复
热议问题