0

I've seen several issues that refer to this problem; but none seem to quite match or fix my situation. On my development machine, I'm not having any problems. When I deploy (to IIS 8.5, Server 2012 R2) I start to get the dreaded parameterless constructor error. Here's some code that I'm using.

public class ReportController : BaseController
{
    public ReportController(KestrelContext ctx) : base(ctx) { }
}

The base controller that I'm deriving from is the following.....

public class BaseController : Controller
{
    protected KestrelContext db;

    public BaseController(KestrelContext ctx)
    {
        db = ctx;
    }
}

And NinjectWebCommon.cs....

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(KestrelAdmin.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(KestrelAdmin.App_Start.NinjectWebCommon), "Stop")]

namespace KestrelAdmin.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using KestrelAdmin.Models;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        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);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<KestrelContext>().ToSelf().InRequestScope();
        }        
    }
}

Target framework is .NET 4.5 all the way around -- not 4.5.x, just 4.5. Verified dependencies in packages.config. Permissions appear to be correct on all files.

Any assistance is greatly appreciated.

UPDATE: I'm completely out of ideas here. As best I can tell, when I deploy to IIS 8.5, the NinjectWebCommon class isn't successfully Initializing the NinjectHttpModule. I'm going to include my package configuration here, and hope that somebody can point me in the right direction.

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="AsyncCTP" version="0.3" targetFramework="net45" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net45" />
  <package id="jQuery" version="1.5.1" />
  <package id="jQuery.UI.Combined" version="1.8.11" />
  <package id="jQuery.Validation" version="1.8.0" />
  <package id="jQuery.vsdoc" version="1.5.1" />
  <package id="log4net" version="2.0.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages.Data" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages.WebData" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Modernizr" version="1.7" />
  <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
  <package id="Ninject" version="3.2.0.0" targetFramework="net45" />
  <package id="Ninject.MVC5" version="3.2.1.0" targetFramework="net45" />
  <package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net45" />
  <package id="Ninject.Web.Common.WebHost" version="3.2.3.0" targetFramework="net45" />
  <package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net45" />
  <package id="Ninject.Web.WebApi.WebHost" version="3.2.4.0" targetFramework="net45" />
  <package id="WebActivatorEx" version="2.0.6" targetFramework="net45" />
  <package id="ZendeskApi_v2" version="3.0.20.0" targetFramework="net45" />
</packages>

Also, I tried the alternative setup described here (with the github binaries) but this resulted in the same error. So now I'm really confused.

sentinel21
  • 546
  • 7
  • 24
  • You can try another approach like: Kernel.Bind().ToConstant(DbContext); – chrjs Jul 27 '15 at 18:36
  • This does not appear to help. – sentinel21 Jul 27 '15 at 20:35
  • 1
    @chrjs: registering a `DbContext` as singleton is a [really really really bad idea](https://stackoverflow.com/questions/3266295/net-entity-framework-and-transactions/3266481#3266481). – Steven Jul 27 '15 at 21:59
  • @Steven thanks for the advice. – chrjs Jul 28 '15 at 07:15
  • I assume you added all Ninject MVC and hosting nugets appropriate for you scenario, right? – Nikolai Samteladze Jul 28 '15 at 11:32
  • @NikolaiSamteladze I assume so. As you can see from my package listing above, I have Ninject, Ninject.MVC5, Ninject.Web.Common, and Ninject.Web.Common.Host I've also added in the WebApi libraries, though I don't really need them. Having them or not having them has not changed anything. – sentinel21 Jul 28 '15 at 15:07

1 Answers1

0

I still don't know what's going on, but somehow I've fixed this. Here's some information on my final configuration. Maybe this will help someone else. I'm sorry I can't be more help; but this just started working for me.

Final packages.config.

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="AsyncCTP" version="0.3" targetFramework="net45" />
  <package id="EntityFramework" version="6.1.3" targetFramework="net45" />
  <package id="jQuery" version="1.5.1" />
  <package id="jQuery.UI.Combined" version="1.8.11" />
  <package id="jQuery.Validation" version="1.8.0" />
  <package id="jQuery.vsdoc" version="1.5.1" />
  <package id="log4net" version="2.0.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages.Data" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages.WebData" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Modernizr" version="1.7" />
  <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
  <package id="Ninject" version="3.2.0.0" targetFramework="net45" />
  <package id="Ninject.MVC5" version="3.2.1.0" targetFramework="net45" />
  <package id="Ninject.Web.Common" version="3.2.0.0" targetFramework="net45" />
  <package id="Ninject.Web.Common.WebHost" version="3.2.3.0" targetFramework="net45" />
  <package id="Ninject.Web.WebApi" version="3.2.4.0" targetFramework="net45" />
  <package id="Ninject.Web.WebApi.WebHost" version="3.2.4.0" targetFramework="net45" />
  <package id="WebActivatorEx" version="2.0.6" targetFramework="net45" />
  <package id="ZendeskApi_v2" version="3.0.20.0" targetFramework="net45" />
</packages>

NinjectWebCommon.cs

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(KestrelAdmin.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(KestrelAdmin.App_Start.NinjectWebCommon), "Stop")]

namespace KestrelAdmin.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using KestrelAdmin.Models;
    using Ninject.Web.Mvc;

    public static class NinjectWebCommon 
    {
        public static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        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);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<KestrelContext>().ToSelf().InRequestScope();
        }        
    }
}

Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using log4net.Config;
using log4net;
using KestrelAdmin.Controllers;
using KestrelAdmin.App_Start;

namespace KestrelAdmin
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(MvcApplication));

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new { controller = @"[^\.]*" }                          // Parameter constraints
            );
        }

        protected void Application_Error()
        {
            var exception = Server.GetLastError();
            var httpException = exception as HttpException;
            Response.Clear();
            Server.ClearError();
            var routeData = new RouteData();
            routeData.Values["controller"] = "Errors";
            routeData.Values["action"] = "Error404";
            routeData.Values["exception"] = exception;
            Response.StatusCode = 500;
            if (httpException != null)
            {
                Response.StatusCode = httpException.GetHttpCode();
                switch (Response.StatusCode)
                {
                    case 403:
                        routeData.Values["action"] = "Error404";
                        break;
                    case 404:
                        routeData.Values["action"] = "Error404";
                        break;
                    case 500:
                        routeData.Values["action"] = "Error404";
                        break;
                }
            }
            log.Fatal("Error " + Response.StatusCode + ": " + exception.Message + " \r\n Occurred on: " + Request.Url + Request.QueryString + " \r\nFrom:" + Request.UserHostAddress + ":" + Request.UserHostName + "\r\n Stack Trace: " + exception.StackTrace);

            IController errorsController = new ErrorController();
            var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
            errorsController.Execute(rc);
        }

        protected void Application_Start()
        {
           // ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(NinjectWebCommon.bootstrapper.Kernel));
            AreaRegistration.RegisterAllAreas();

            log4net.Config.XmlConfigurator.Configure();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            XmlConfigurator.Configure();
        }
    }
}

If someone else has a clue what I've done (if anything) that helped, answer and I'll give you the points for this question.

sentinel21
  • 546
  • 7
  • 24
  • 1
    Check your web.config files for assembly redirects. I found if these were slightly out, or used the wrong version, ninject would not load correctly & the default controller factory would be used - hence the constructor error. – Simon Halsey Aug 05 '15 at 23:29
  • Simon Halsey, what does Check your web.config files for assembly redirects actually mean please? – Mike Cave Apr 19 '18 at 13:36