I have a web application that uses .NET Framework 4, MVC 4, Ninject 3. Originally I was using .NET Framework 4.5 but I changed the “Target Framework” to 4.0 and fixed all the errors it caused. Now the application runs perfectly on my local box. The problem is that when I deploy it to Azure I get the server error at the bottom of this post.
I have seen this error before. When I was first starting to use Ninject I would get this error when I wasn’t binding correctly in the Global.asax (relevant code below).
Also, my Ninject.dll reference “Copy Local” property is set to “True”.
What am I doing wrong? Why am I getting this error when I try to view the web application in Azure but not on my local box?
Thank you for your help,
Aaron
HomeController.cs:
public class HomeController : Controller
{
IAuthorizationService _authorizationService;
IUserService _userService;
public HomeController(IAuthorizationService authorizationService, IUserService userService)
{
_authorizationService = authorizationService;
_userService = userService;
}
}
Global.asax:
protected void Application_Start()
{
SetupDependencyInjection();
}
private void SetupDependencyInjection()
{
//create Ninject DI Kernel
IKernel kernel = new StandardKernel();
//register services with Ninject DI container
RegisterServices(kernel);
//tell asp.net mvc to use our Ninject DI Container
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
private void RegisterServices(IKernel kernel)
{
kernel.Bind<IAccountService>().To<AccountService>().WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["AccountManagerEntities"].ConnectionString);
}
Server Error:
No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[MissingMethodException: No parameterless constructor defined for this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +117
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +247
System.Activator.CreateInstance(Type type, Boolean nonPublic) +106
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +84
[InvalidOperationException: An error occurred when trying to create a controller of type 'AccountManager.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +247
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +85
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +280
System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +66
System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +19
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +161
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +405
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375
Sandrino Di Mattia, you are correct, I need to register the other interfaces. Actually, I do have them registered. For the sake of being to-the-point in my example code I cut out portions that were relevant. :p
The way I fixed it was merely uninstalling Ninject with NuGet and then re-installing it with NuGet. I had read an article on the internet that said that I’d have to do this with Entity Framework to allow it to install properly with .NET 4.0 after re-targeting from 4.5. So, when that solved my EF problems I tried it with Ninject and it worked.
So, the moral of all this is when re-targeting from .NET 4.5 to .NET 4.0, uninstall your NuGet packages and then re-install them.
Aaron