title: ASP.NET MVC 4 Ninject MVC 4 Default constructor not found for type 'App.Controller'
body: <p>I have set up a new ASP.NET MVC 4 Application in Xamarin Studio / monodevelop-opt on Ubuntu 14.04 LTS (Mono 3.10.0 mod-mono-server4 / xsp when running in IDE). Packages Installed are:</p>
  
  <pre><code>Install-Package Microsoft.AspNet.Mvc -Version 4.0.40804
  </code></pre>
  
  <ul>
  <li>Microsoft.AspNet.MVC</li>
  <li>Microsoft.AspNet.Razor</li>
  <li>Microsoft.AspNet.WebPages</li>
  <li>Microsoft.Web.Infrastructure</li>
  </ul>
  
  <p>I also had to install Optimization Framework:</p>
  
  <pre><code>Install-Package Microsoft.AspNet.Web.Optimization
  </code></pre>
  
  <p>I chose to implement IoC container Ninject from NuGet and following packages were installed:</p>
  
  <pre><code>Install-Package Ninject.Mvc4
  </code></pre>
  
  <ul>
  <li>Ninject</li>
  <li>Ninject.MVC4</li>
  <li>Ninject.Web.Common.WebHost</li>
  <li>Ninject.Web.Common</li>
  </ul>
  
  <p>Because I installed Ninject.Mvc4, it created a nice file for me in App_Start called NinjectWebCommon.cs</p>
  
  <p>Here is the Create Kernal Method:</p>
  
  <pre><code>    private static IKernel CreateKernel ()
      {
          var kernel = new StandardKernel ();
          try {
              kernel.Bind&lt;Func&lt;IKernel&gt;&gt; ().ToMethod (ctx =&gt; () =&gt; new Bootstrapper ().Kernel);
              kernel.Bind&lt;IHttpModule&gt; ().To&lt;HttpApplicationInitializationHttpModule&gt; ();
  
              RegisterServices (kernel);
              return kernel;
          } catch {
              kernel.Dispose ();
              throw;
          }
      }
  </code></pre>
  
  <p>Here is the Register Services Method:</p>
  
  <pre><code>    private static void RegisterServices (IKernel kernel)
      {
          kernel.Bind&lt;IResourceEntryService&gt; ().To&lt;ResourceEntryService&gt; ();
  
          var modules = new List&lt;INinjectModule&gt; {
              new ConfigModule (),
              new RepositoryModule (),
              new LoggingModule ()
          };
  
          kernel.Load (modules);
      }
  </code></pre>
  
  <p>Resource Entry Service and Interface:</p>
  
  <pre><code>public interface IResourceEntryService
  {
      IEnumerable&lt;ResourceEntry&gt; GetResourceEntries ();
  
      IEnumerable&lt;ResourceEntry&gt; GetResourceEntriesByNameAndCulture (string name, string culture);
  }
  
  public class ResourceEntryService : IResourceEntryService
  {
      IResourceEntryRepository _resourceEntryRepository;
  
      public ResourceEntryService (IResourceEntryRepository resourceEntryRepository)
      {
          _resourceEntryRepository = resourceEntryRepository;
      }
  
      #region IResourceEntryService implementation
  
      public System.Collections.Generic.IEnumerable&lt;ResourceEntry&gt; GetResourceEntries ()
      {
          IEnumerable&lt;ResourceEntry&gt; resourceEntries = _resourceEntryRepository.GetResourceEntries ();
          return resourceEntries;
      }
  
      public System.Collections.Generic.IEnumerable&lt;ResourceEntry&gt; GetResourceEntriesByNameAndCulture (string name, string culture)
      {
          IEnumerable&lt;ResourceEntry&gt; resourceEntries = _resourceEntryRepository.GetResourceEntriesByNameAndCulture (name, culture);
          return resourceEntries;
      }
  
      #endregion
  }
  </code></pre>
  
  <p>Controller for Resource Entry passing new model object into view:</p>
  
  <pre><code>public class ResourceEntryController : Controller
  {
      IResourceEntryService _resourceEntryService;
  
      public ResourceEntryController (IResourceEntryService resourceEntryService)
      {
          _resourceEntryService = resourceEntryService;
      }
  
      public ActionResult Index ()
      {
          ResourceEntryViewModel viewModel = new ResourceEntryViewModel ();
  
          return View (viewModel);
      }
  }
  </code></pre>
  
  <p>Here is my ~/Views/ResourceEntry/Index.cshtml file:</p>
  
  <pre><code>@model App.Web.UI.ViewModels.ResourceEntryViewModel
  
  &lt;h1&gt;Resource Page&lt;/h1&gt;
  </code></pre>
  
  <p>So now everything looks Okay right? Well wrong! I get the following error message when I try and view that page.</p>
  
  <pre><code>System.MissingMethodException
  Default constructor not found for type App.Web.UI.Controllers.ResourceEntryController
  
  at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00094] in /usr/src/packages/BUILD/mcs/class/corlib/System/Activator.cs:326 
  at System.Activator.CreateInstance (System.Type type) [0x00000] in /usr/src/packages/BUILD/mcs/class/corlib/System/Activator.cs:222 
  at System.Web.Mvc.DefaultControllerFactory+DefaultControllerActivator.Create (System.Web.Routing.RequestContext requestContext, System.Type controllerType) [0x00000] in &lt;filename unknown&gt;:0 
  
  Version Information: 3.10.0 (tarball Sat Oct 4 16:28:24 UTC 2014); ASP.NET Version: 4.0.30319.17020
  Powered by Mono
  </code></pre>
  
  <p>Anyone have any ideas how to set up Ninject for MVC 4 application running on a mono stack.</p>
  
  <p>I had a good google'ing and stackoverlow'ing and cant really find a definitive answer. Everything links to Web API, well im not really interested in Web API as I will be using ServiceStack if it comes to that.</p>
  
  <p>I just want this Web application to work. Anyone have any suggestions?</p>
  
  <p><strong>Update: 20/12/2014</strong></p>
  
  <p>Runs fine under windows but not Ubuntu</p>
  
  <p>I have created a simple application which can be found here:</p>
  
  <p><a href="https://github.com/croconoster/MVC4_IoC" rel="nofollow">Sample Application</a></p>
  
  <p>Any Ideas?</p>
  
  <p><strong>Update 21/12/2014</strong></p>
  
  <p>I have tried another implementation of an IoC Container.</p>
  
  <p>Same thing happens, I think this might be a Mono MVC 4 Thing. Still no idea why.</p>
  
  <p><strong>Update 22/12/2014</strong></p>
  
  <p>It looks like App_Start does not get invoked on start up. </p>
  
  <p>I did a <code>Console.WriteLine</code> in the <code>CreateKernel</code> and in <code>RegisterServices</code> and nothing printed out.</p>
  
  <p>I could get <a href="https://www.nuget.org/packages/SimpleInjector.MVC3/" rel="nofollow">SimpleInjector</a> working by configuring the Container in the Global.ascx.cs file. It won't work if it gets initialized in the App_Start with the following line at the top.</p>
  
  <pre><code>[assembly: WebActivator.PostApplicationStartMethod (typeof(App.Web.UI.App_Start.SimpleInjectorInitializer), "Initialize")]
  </code></pre>
  
  <p>Wonder if there is a problem with WebActivator with Mono.</p>