Microsoft Extensions Dependency Injection (DI) with Sitecore 8.2 Sample Project

Sitecore 8.2 was released with Microsoft Dependency Injection. I wanted to try to setup a sample project which utilizes the DI.

First and foremost, I thank Kam for writing the ServiceCollectionExtensions and making my life easier.

As with everything we start with a fresh install of Sitecore 8.2 (sc82rev160729). Following which setup a base visual studio project. I setup a very basic controller rendering to test this out. Here is the setup for the functionality of the TextBlock. The code necessary for Glass can be found in my previous blog posts. Later on I will get into the config.


Here is a simple Business Interface and class implementations:

Lets go ahead and define Controller and the View:

Now that we have everything set in terms of functionality. We need plumbing (funnel connector) to register with the DI. We do this by implementing an IServicesConfigurator and later on we will add the config to get this working.

Now that we registered all the items we need, next is the config (which is one of a few ways) to let Sitecore know:

That is it. I could not believe how simple this was. If you have more registrations, simple add it to the Configure method. When its all said and done, here is the output.
Make sure all the interfaces you need are registered. Check the /sitecore/admin/ShowServicesConfig.aspx page for registrations via the Microsoft DI in Sitecore ONLY. If you use a different DI framework, it will not show your registrations on this page.



If you have any questions or concerns, please get in touch with me. (@akshaysura13 on twitter or on Slack).

Documentation on Microsoft DI:


Add your comment

Your email address will not be published.