I have two MVC projects that have identical configuration and they use Unity for depending injection.
The first has 10 controllers and the second one has 20 controllers and dependency injection works perfectly for all of these and has been working well for over a year now.
Today I tried to copy 1 controller from the first project to the second and when I run it I get:
[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, StackCrawlMark& stackMark) +113
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
System.Activator.CreateInstance(Type type) +6
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55
[InvalidOperationException: An error occurred when trying to create a controller of type 'Web.Admin.Controllers.ApplyController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +179
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +197
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +49
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
It looks as though for that controller only it applies the DefaultcontrollerActivator instead of the unity one.
I have been going the web.config for over an hour and it seems just fine, unity is registered and all the dependencies, and the Application_Start is identical to the one of the the first project that works with the same controller. I can't possibly imagine why everything would work except that one controller.
I tried simplifying the controller to bare basics and this still happens.
I post some code below but I don't think it'll help because I have an identical instance that works and one that doesn't. Either way the application start looks like this:
var container = Ioc.ContainerWrapper.Container as IUnityContainer;
container
.ConfigureAutoRegistration()
.ExcludeAssemblies(t => !t.FullName.StartsWith("Alpari."))
.Include(If.ImplementsITypeName, Then.Register())
.ApplyAutoRegistration();
container.RegisterType<INHibernateContext, NHibernateContext>();
container.RegisterType<HttpContextBase>(new InjectionFactory(x => new HttpContextWrapper(HttpContext.Current)));
container.RegisterType<IAuthentication, FormsAuthenticationWrapper>();
container.RegisterType<IApplyService, ApplyService>();
/* register loads of interfaces */
AreaRegistration.RegisterAllAreas();
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new LocalizedViewEngine());
DependencyResolver.SetResolver(new UnityDependencyResolver());
The dependency resolver is applied here and this works for all controllers except the ApplyController which seems to be trying to use the default resolver.
The constructor has 10 interfaces but I tried just doing this and I get the same error:
public ApplyController(IApplyService applyService)
{
this.applyService = applyService;
}
At this point I need suggestions as to what could be wrong or how to debug this.