Sitecore WebAPI – Setting up IOC

I’ve had the opportunity to tinker in the depths of the WebAPI in Sitecore 7.5 and 8 recently. I’ll put up a post about setting up controllers easily shortly but until then I thought I’d share how to setup IOC.

In a normal WebAPI environment, we can create our resolver and attach it to the GlobalConfiguration. However, we must remember that Sitecore uses its own pipelines for MVC and WebAPI and so setting this up in the Global ASAX won’t work.

The way I’ve done it is to change the registered WebAPI initialiser class in the Sitecore.Services.Config file found in App_Config/Include. This lets me hook directly into the entry point for Sitecore related WebAPI and adjust the configuration accordingly.

The custom initialiser looks like

   public class CustomInitialiser
      public void Process(PipelineArgs args)
         // This are the existing steps
         IHttpConfiguration servicesConfig = new ApplicationContainer().ResolveServicesWebApiConfiguration();
         HttpConfiguration httpConfig = servicesConfig.Configure(GlobalConfiguration.Configuration, RouteTable.Routes);

         Sitecore.Diagnostics.Log.Info("finished initialising the web api.", this);
         httpConfig.DependencyResolver = new CustomIocResolver(GlobalContainerReference);

Where the variable ‘GlobalContainerReference’ is the hook onto whatever IOC you have. As we’re in a Sitecore loaded pipeline we’ll need to load this via some static reference. So you’ll need to create one if you want to follow this method. An alternative would be to put an interface onto your Application class that lets you access the container and then use that.

The other class I have shown here is the ‘CustomIocResolver’. Now depending on which container library you are using, you may need to write your own. I prefer AutoFac in most cases but many of my customers use Unity and if you are, you’ll need to write your own.

A simple example of one is on the ASP.NET website

To make use of this class in Sitecore, simply swap out the setting in the services config. The original entry is a patch so you can either update that or create your own include file. Where possible I use include files so if you are unsure on how to do this I strongly recommend you read up on it!

<processor type="Common.Mvc.Ioc.Pipelines.WebApiInitialiser, Common.Mvc" patch:after="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />

Hope this saves you some time!



3 thoughts on “Sitecore WebAPI – Setting up IOC

  1. Nice post Simon. I’ll have to give this a try.
    I’ve been playing with WebApi on Sitecore 7.1, so I don’t know much about the differences between that and later versions.

    You mentioned not attaching to the GlobalConfiguration because it’s outside the Global.asax. Can we still do this, just in a pipeline step (as you demonstrated)?
    Is that what the ApplicationContainer does? Is that your own class? Because I don’t think I’ve seen it before.

    Also, have you worked on getting constructor injection working? I’ve found this doesn’t come out of the box with Dependency Injection, and we need custom Controller Factories in addition to all this stuff…

    It adds a bit of complexity to all this setup. 🙂

    Thanks again for the post. It’s definitely helping me solve the IoC puzzle with Sitecore. 🙂

  2. Hi,

    Based on my current tests, this should give you constructor injection as you’re writing your own dependency resolver and attaching it to the HttpConfiguration. Think of this in a similar way to the controller factory system for mainline MVC.

    ApplicationContainer() is in the Sitecore API in the Sitecore.Services namespace. There’s 3-4 dlls in there so its worth having a scan.

    This initialisation is all done in a Sitecore pipeline. If you look at the Sitecore.Services.Config you’ll see where the above class gets hooked in.

    I’m going to do another post about how to hook into the routing and how to add your own WebAPI route reliably as well as use the ServiecsApiController attribute. This is a bit more reliable than staying in the Global Asax (for me anyway!)

    Stay tuned 🙂

  3. Pingback: Safe Dependency Injection for MVC and WebApi within Sitecore | Sean Holmesby

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s