Dependency Injection Works in Dev but not Production

You may run into a strange error if you're using code similar to Kam's example code for wiring up dependency injection in Sitecore. You will run into a situation where dependency injection is working great on a development machine but suddenly stops working in staging or production.

As you investigate further with Sitecore's admin tools you'll realize that it's only controllers that have the problem. It turns out that on release builds with the Optimize Code checkbox checked breaks the assembly naming convention used that's used.

The fix is fairly simple. Replace the original sample code:

serviceCollection.AddMvcControllersInCurrentAssembly();

with this:

serviceCollection.AddMvcControllers(typeof(ServicesConfigurator).Assembly);

* Sitecore StackExchange Answer

Dependency Injection Works in Dev but not Production
Share this