Challenge:
The best part of our lives is, Whatever/However experience you are you will face one exception/error which challenges your experience. And that’s what happened with me recently. And I am sure you agree — That’s when your basics come to rescue — Same was the case in my case.
We were deploying Helix based solution which was working like a charm in local. [In case you haven’t heard of Helix/Habitat yet – Which I don’t expect to be the case. But If heard and not explored/clear on it. Then this post might help you]
But as you know “life is not as easy as it seems to be” When we deployed it on Dev/QA environment. We met following error:
Sitecore.Mvc.Diagnostics.ControllerCreationException was unhandled by user code
ControllerName=Navigation
HResult=-2146233088
Message=Could not create controller: ‘Navigation’.
The item being rendered is: ‘/sitecore/content/SCBasics/Home’.
The context item is: ‘/sitecore/content/SCBasics/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.
Source=Sitecore.Mvc
StackTrace:
at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at Sitecore.Mvc.Controllers.ControllerRunner.GetController()
at Sitecore.Mvc.Controllers.ControllerRunner.Execute()
at Sitecore.Mvc.Presentation.ControllerRenderer.Render(TextWriter writer)
at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Render(Renderer renderer, TextWriter writer, RenderRenderingArgs args)
at Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer.Process(RenderRenderingArgs args)
at (Object , Object[] )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
at Sitecore.Mvc.Pipelines.Response.RenderPlaceholder.PerformRendering.Render(String placeholderName, TextWriter writer, RenderPlaceholderArgs args)
at (Object , Object[] )
at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
at Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
at Sitecore.Mvc.Helpers.SitecoreHelper.Placeholder(String placeholderName)
at ASP._Page_Views_Website_Layouts_Default_cshtml.Execute() in c:\…..\Website\Views\Website\Layouts\Default.cshtml:line 38
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
at Sitecore.Mvc.Presentation.ViewRenderer.Render(TextWriter writer)
InnerException:
HResult=-2146233079
Message=An error occurred when trying to create a controller of type ‘SCBasics.Feature.Navigation.Controllers.NavigationController’. Make sure that the controller has a parameterless public constructor.
Source=System.Web.Mvc
StackTrace:
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName)
InnerException:
HResult=-2147467261
Message=Context is null
Source=Glass.Mapper
StackTrace:
at Glass.Mapper.AbstractService..ctor(Context glassContext) in c:\TeamCity\buildAgent\work\8567e2ba106d3992\Source\Glass.Mapper\AbstractService.cs:line 103
at lambda_method(Closure , ServiceProvider )
at Sitecore.Mvc.Controllers.SitecoreDependencyResolver.GetService(Type serviceType)
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
InnerException:
You might say that’s easy “controller has a parameterless public constructor.” – Just add this and it should work 🙂 But same code works in local, and second thing is we are using Sitecore 8.2 DI for controllers. And in that case, it is not required to have parameter less public constructor. And if it is same error for you, it means that, your DI is not working.
You are also facing same error? Then this post might help you!
Solution:
So, as every developer on this earth does, We also did a quick Google search and found following links, which were helpful:
- http://sitecore.stackexchange.com/questions/2929/no-parameterless-constructor-defined-for-this-object-after-upgrade-to-sitecore8 – dnstommy’s reply gave us a hint that our DI is not working
- Apart from this there were no any good results on web. So, now it was time to look back in code and see how things laid down and figuring out what’s going on
Troubleshooting process:
- We use Configurator approach for DI and our Configurator class calls — Kam’s ServiceClassExtension method which resides under and we call it from feature using serviceCollection.AddMvcControllersInCurrentAssembly();– Refer : http://sitecore.stackexchange.com/questions/4370/few-questions-related-to-helix
- Then I thought to check what’s going on with AddMvcControllersInCurrentAssembly – Which is in Foundation layer, getting called by feature layer
public static class ServiceCollectionExtensions { public static void AddMvcControllersInCurrentAssembly(this IServiceCollection serviceCollection) { AddMvcControllers(serviceCollection, Assembly.GetCallingAssembly()); } // Removed for simplicity public static void AddMvcControllers(this IServiceCollection serviceCollection, params Assembly[] assemblies) { var controllers = GetTypesImplementing<IController>(assemblies) .Where(controller => controller.Name.EndsWith("Controller", StringComparison.Ordinal)); foreach (var controller in controllers) { serviceCollection.AddTransient(controller); } } // Removed for simplicity }
- Looking at code, it makes sense. But it works in local. But not when deployed. So, after troubleshooting a bit. Thought to log what we get in calling assembly — We also faced challenge here. Sitecore logging will not work at this layer. So, we had to use plain old StreamWriter [You remember, I told. Basics!?]
- The log results were something like this:
- In local : In Calling assembly we were getting Feature assemblies
- In QA/Dev : In Calling assembly we were getting Sitecore.Kernel
- It was clear, Assembly.GetCallingAssembly() was misbehaving
- Quick Google search reveled the mystery, GetCallingAssembly works different in Debug and Release mode https://blog.codeinside.eu/2014/10/05/Be-Aware-Of-Asssembly-GetCallingAssembly-Behaviour/
- In local we are using Debug mode and on QA/Dev – We are using Release mode
- Root cause “The JIT compiler moves code around to optimize for performance. Small methods (up to about 56 Byte IL-Code if I remember it right) can be inlined where the method call was before. But the compiler does this only in release, not in debug mode. Also when attaching the debugger to our release build the JIT compiler stopped inlining to enable debugging and our bug was gone” : Source : http://www.ticklishtechs.net/2010/03/04/be-careful-when-using-getcallingassembly-and-always-use-the-release-build-for-testing/
- Last post had an approach — Adding [MethodImplAttribute(MethodImplOptions.NoInlining)] on method — Which somehow didn’t work for us
- But then thought to look for some better options and we found it!
- We added one more Service configurator in Foundation which has following code:
namespace SCBasics.Foundation.DependencyInjection.Services { public class RegisterAllFeatureControllers : IServicesConfigurator { public void Configure(IServiceCollection serviceCollection) { /* We were facing issue in Release mode Which was not resolving */ serviceCollection.AddMvcControllers("SCBasics.Feature.*"); } } }
- Above code, uses an alternate method for adding MVC Controllers and best part of this approach is. You no need to register your controllers, which you added in feature project. This foundation project will automatically do it! [BTW, Nothing is automatic, we wrote one time code for it ;-)]
Happy Coding! 🙂