This project is read-only.

No parameterless constructor defined for this object

Oct 9, 2014 at 12:38 PM
Edited Oct 9, 2014 at 12:49 PM
I use nuget simple injector mvc integration quick start to install simple injector v2.5.2 and related packages to my helloworld mvc project. It works fine in my local express IIS and local IIS. But when I upload it to production IIS server, it throws exception
    [MissingMethodException: No parameterless constructor defined for this object.]
     .....
    Make sure that the controller has a parameterless public constructor
The SimpleInjectorInitializer class code snippet as below.
            var container = new Container();
            container.Register<IService, Service>();
            container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
            container.Verify();
            DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
This stackoverflow post can't help me, DependencyResolver.SetResolver is already called.

How to fix it? Did my production IIS miss any configuration?
Oct 9, 2014 at 9:57 PM
Did you add a virtual directory for your site? I expect that you placed your code in a sub folder without marking this as virtual directory.
Nov 11, 2014 at 7:15 PM
I am getting the same error and created a basic MVC project where the issue can be replicated,
I've been able to track it down to the build configuration.

If compiled in Debug (Code Optimization: off, Debug Info: full), SimpleInjector works properly.
However if compiled in Release configuration the "No parameterless constructor" exception occurs.

The temporary workaround is to compile in Debug configuration until the issue is resolved.

.NET 4.5.1, Simple Injector 2.6.1, ASP.NET MVC 5.2.0, ASP.NET WebApi 2.2, deployed as an IIS 8.5 Web Application

Any thoughts? Suggestions?
Nov 12, 2014 at 2:33 AM
Edited Nov 12, 2014 at 2:35 AM
fredmerlo wrote:
I am getting the same error and created a basic MVC project where the issue can be replicated,
I've been able to track it down to the build configuration.

If compiled in Debug (Code Optimization: off, Debug Info: full), SimpleInjector works properly.
However if compiled in Release configuration the "No parameterless constructor" exception occurs.

The temporary workaround is to compile in Debug configuration until the issue is resolved.

.NET 4.5.1, Simple Injector 2.6.1, ASP.NET MVC 5.2.0, ASP.NET WebApi 2.2, deployed as an IIS 8.5 Web Application

Any thoughts? Suggestions?
In my simple MVC application I am using a Singleton to encapsulate the container initialization, the singleton is invoked in the Global.asax. Changing the singleton to use Static Initialization forces the container to initialize "immediately" on application startup, thus resolving the issue. The mystery of why a Debug build configuration causes different singleton initialization behavior from a Release configuration will be left for another day.

Below is an example of a static initialization singleton, hope this helps.
public sealed class Singleton
{
   private static readonly Singleton instance = new Singleton();
   
   private Singleton()
   {
      //DI registration goes here
   }

   public static Singleton Instance
   {
      get 
      {
         return instance; 
      }
   }
}