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:

using SC82.Models.Templates.Content;

namespace SC82.Business.Content
{
    public interface IContentLogic
    {
        ITextBlock GetTextBlock();
    }
}

using SC82.Business.GlassSC;
using SC82.Models.Templates.Content;

namespace SC82.Business.Content
{
    public class ContentLogic : IContentLogic
    {
        private readonly IControllerSCContext _currentContext;

        public ContentLogic(IControllerSCContext currentContext)
        {
            _currentContext = currentContext;
        }

        public ITextBlock GetTextBlock()
        {
            return _currentContext.GetDataSource();
        }
    }
}

Lets go ahead and define Controller and the View:

using SC82.Business.Content;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SC82.Controllers
{
    public class ContentController : Controller
    {
        private readonly IContentLogic _contentLogic;

        public ContentController(IContentLogic contentLogic)
        {
            _contentLogic = contentLogic;
        }

        public ActionResult RenderTextBlock()
        {
            return View(_contentLogic.GetTextBlock());
        }
    }
}
@inherits Glass.Mapper.Sc.Web.Mvc.GlassView
@using Sitecore.Mvc

@if (Model != null)
{
    

@Editable(Model, y => y.Heading)

@Editable(Model, y => y.SubHeading)

@Editable(Model, y => y.Content)
}

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.

using Glass.Mapper.Sc;
using Microsoft.Extensions.DependencyInjection;
using SC82.Business.Content;
using SC82.Business.GlassSC;
using Sitecore.DependencyInjection;
using Sitecore.Foundation.DependencyInjection;

namespace SC82.Business.DI
{
    public class RegisterDI : IServicesConfigurator
    {
        public void Configure(IServiceCollection serviceCollection)
        {

            var serviceProvider =  serviceCollection.BuildServiceProvider();
            
            serviceCollection.AddTransient(provider => new SitecoreContext());
            serviceCollection.AddTransient(provider => new GlassHtml(serviceProvider.GetService()));
            serviceCollection.AddTransient(provider => new ControllerSCContext(serviceProvider.GetService()));
            serviceCollection.AddTransient();

            serviceCollection.AddMvcControllers(
                "SC82*");
        }
    }
}

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

6 thoughts on “Microsoft Extensions Dependency Injection (DI) with Sitecore 8.2 Sample Project”

  1. 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?

  2. @Ethan Schofer, did you ever get this resolved? I am experiencing the same issue in my local environment and I have narrowed it down to an issue between the version of MSBuild which VS 2015 uses and the .Net 4.6.2 framework.

  3. @Ethan @mike I use CI with VSTS and deploy using Octopus and I do not face any issues. I am wondering if its to do with the .net version on the TeamCity build server.

  4. I resolved this pointing to the Microsoft Dependency Injection dlls that shipped with Sitecore instead of trying to add the correct version with NuGet.

Leave a Reply

Your email address will not be published. Required fields are marked *