Search code examples
sitecoresimple-injectorglass-mappersitecore-mvcsitecore8.1

Constructor on type 'CommonController' not found issue in Simple Injector Sitecore 8.1 MVC


I have implemented Simple Injector in Sitecore 8.1 MVC glass mapper v4 project and using Controller rendering on MVC Layout.When I run the page getting following error:

Server Error in '/' Application.

Constructor on type 'GlassSimpleInjector.Controllers.CommonController' not found. 
  Description: An unhandled exception occurred. 

 Exception Details: System.MissingMethodException: Constructor on type 'GlassSimpleInjector.Controllers.CommonController' not found.

Source Error: 



Line 13:         <h1>@Html.Sitecore().Field("title")</h1>
Line 14:         <div>
Line 15:             @Html.Sitecore().Placeholder("main")
Line 16:         </div>
Line 17:     </body>


 Source File:  c:\inetpub\wwwroot\GlassSimpleInjector\Website\Views\Shared\MVC Layout.cshtml    Line:  15 

Stack Trace: 



[MissingMethodException: Constructor on type 'GlassSimpleInjector.Controllers.CommonController' not found.]
   System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark) +14259449
   System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +200
   System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture) +28
   Sitecore.Mvc.Helpers.TypeHelper.CreateObject(Type type, Object[] parameters) +43
   Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateControllerInstance(RequestContext requestContext, String controllerName) +330
   Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName) +122

[ControllerCreationException: Could not create controller: ' GlassSimpleInjector.Controllers.CommonController,GlassSimpleInjector'. 
The context item is: '/sitecore/content/Home'. 
The current route url is: '{*pathInfo}'. This is the default Sitecore route which is set up in the 'InitializeRoutes' processor of the 'initialize' pipeline. ]
   Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName) +371
   Sitecore.Mvc.Controllers.ControllerRunner.GetController() +24
   Sitecore.Mvc.Controllers.ControllerRunner.Execute() +54
   Sitecore.Mvc.Presentation.ControllerRenderer.Render(TextWriter writer) +131
   Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args) +27
   Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Process(RenderRenderingArgs args) +62
   (Object , Object[] ) +83
   Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) +369
   Sitecore.Mvc.Pipelines.PipelineService.RunPipeline(String pipelineName, TArgs args) +202
   Sitecore.Mvc.Pipelines.Response.RenderPlaceholder.PerformRendering.Render(String placeholderName, TextWriter writer, RenderPlaceholderArgs args) +192
   (Object , Object[] ) +83
   Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) +369
   Sitecore.Mvc.Pipelines.PipelineService.RunPipeline(String pipelineName, TArgs args) +202
   Sitecore.Mvc.Helpers.SitecoreHelper.Placeholder(String placeholderName) +323
   ASP._Page_Views_Shared_MVC_Layout_cshtml.Execute() in c:\inetpub\wwwroot\GlassSimpleInjector\Website\Views\Shared\MVC Layout.cshtml:15
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +126
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +125
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +147
   Sitecore.Mvc.Presentation.ViewRenderer.Render(TextWriter writer) +258

I have done almost all configuration but don't know what is missing. Any suggestion. I have implemented the solution from this link

Simple Injector

Update as on 11/24/2015

Just want to throw some light on structure I have two MVC project:

GlassSimpleInjector.Global -->I did basic SimpleInjector implementation here and added SimpleInjector.cs class inside App_Start folder which contains common functionality and registered ISitecoreService and ISitecoreContext

GlassSimpleInjector --> I have added SimpleInjector.cs file inside App_Start where I have registered interface and classes only speicific to this project

SimpleInjector.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
               xmlns:set="http://www.sitecore.net/xmlconfig/set/">>
  <sitecore>
    <pipelines>
      <initialize>
        <processor type="Sitecore.Mvc.Pipelines.Loader.InitializeControllerFactory, Sitecore.Mvc"
                   set:type="GlassSimpleInjector.Global.Pipelines.DI.InitializeSimpleInjectorControllerFactory, GlassSimpleInjector.Global" />
      </initialize>
    </pipelines>
  </sitecore>
</configuration>

Solution

  • Are you sure your controller factory is getting registered? According to the stack trace SimpleInjectorControllerFactory is not being invoked. Try checking the showConfig page to see if your processor is getting patched into the initialize pipeline correctly.

    Update in response to comment from OP

    The first block of code in Akshay's post is a Sitecore config patch file that needs to go in the /App_Config/Include folder. Note that you will need to put it in an additional subfolder or name the file so that it comes alphabetically after the Sitecore.Mvc.config in order for it to correctly patch the original processor. If you go to /sitecore/admin/showconfig.aspx in a browser, you can see the merged configuration. You should see your custom processor in the place of the original InitializeControllerFactory processor in the initialize pipeline. If it's not there, then your controller factory won't get registered.

    Update The problem is an extra right angle bracket in the config patch file.