0

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Nick
  • 2,877
  • 2
  • 33
  • 62
  • The Ioc cannot resolve one or more objects has been defined in the constructor of ApplyController. You should post the constructor of ApplyController as well. – Toan Vo Aug 06 '13 at 10:31
  • Are you sure? I tried loading Applycontroller using only one interface and I get the very same problem. – Nick Aug 06 '13 at 10:32
  • What's interface object you has been called in ApplyController? – Toan Vo Aug 06 '13 at 10:32
  • That sounds a lot like [this problem](http://stackoverflow.com/questions/15908019/simple-injector-unable-to-inject-dependencies-in-web-api-controllers). – Steven Aug 06 '13 at 10:33
  • I just updated my question. For example I tried using the ApplyService. It's registered in the Application start and works well on project 1 – Nick Aug 06 '13 at 10:34
  • Well, you has been registered the IApplyService but it still cannot resolve the IApplyService properly. You should try by separate resolve the IApplyService to make sure it will be resolved by unity as expected. – Toan Vo Aug 06 '13 at 10:36

2 Answers2

1

Try resolving the ApplyController directly from the Unity container (by calling container.Resolve<ApplyController>()). This call will fail with a descriptive exception that will explain the real problem.

For more information about why this is happing, read this.

The constructor has 10 interfaces

Not related to your problem but this sounds like a violation of the Single responsibility principle. Classes that has that many dependencies are often hard to test, read and maintain. In the future, you should try to get the number of dependencies down.

Community
  • 1
  • 1
Steven
  • 166,672
  • 24
  • 332
  • 435
0

Referring your following code; I presume the issue with related to the construction phase of ApplyService. This means, container is unable to instigate ApplyService when resolved through ApplyController. You can check whether the construction chain has any hierarchical dependency, those are failing.

container.RegisterType<IApplyService, ApplyService>();
S.N
  • 4,910
  • 5
  • 31
  • 51