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.

di1
di2

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.
di3
 
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.

di5

di8
di9
di10

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

Documentation on Microsoft DI: https://docs.asp.net/en/latest/fundamentals/dependency-injection.html

3 comments

  • Bill Dinger 10 months ago

    Might be worth noting, general docs on the .NET core DI can be found here (https://docs.asp.net/en/latest/fundamentals/dependency-injection.html) if you want to use stock instead of replacing it with your own like windsor, simpleinjector, etc

    For the services node, did Sitecore hook into that for *all* object creation, or only controller/pipelines?

  • akshaysuraadmin 10 months ago

    Bill, thank you. I will add the link. Also Sitecore is slowing adding in injectable code as they do the releases, it not limited to controller/pipelines. You could get more information using this link: http://kamsar.net/index.php/2016/08/Dependency-Injection-in-Sitecore-8-2/

    I also updated a screenshot of the registered items in sitecore on the /sitecore/admin/ShowServicesConfig.aspx page.

  • Ethan Schofer 2 months ago

    I am having a problem implementing this. It works locally but on my build server I get conflicts around some of the other DLLs that get installed when I add Microsoft.Extensions.DependencyInjection:

    error CS1703: Multiple assemblies with equivalent identity have been imported: ‘E:\TeamCity\buildAgent\work\533f339a08666e77\packages\System.Runtime.Extensions.4.1.0\lib\net462\System.Runtime.Extensions.dll’ and ‘C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\Facades\System.Runtime.Extensions.dll’. Remove one of the duplicate references. [E:\TeamCity\buildAgent\work\533f339a08666e77\src\Framework\DD.Framework.DotNet\code\DD.Framework.DotNet.csproj]

    Have you used Microsoft DI in a CI environment? Did you come across this issue?

Add your comment

Your email address will not be published.