Tip: Move your IoC from Global.asax!

So recently I was upgrading one of our Sitecore 6.5 instances up to Sitecore 7 and came a cropper in one of the upgrades because the Global.asax file was included and overwrote the one we had written! Given I didn’t want this to happen in the future I sought about finding a better home for where we set up our inversion of control. After some asking around a colleague mention this blog post to me, specifically pointing out PreApplicationStartMethodAttribute which is a little know golden nugget in .NET.

Essentially this attribute lets you run code way before even Application_Start and can be used in a standalone class library. With this new attribute in my power I ended up creating an Initialise class library and moved all the IoC setup code to a class in there. Then added the attribute to the AssemblyInfo.cs class added a reference to the new library in the website project and everything worked like a charm!

Oh and thanks to the helpful people at Sitecore support who said they’ll be including the fact the Global.asax gets overwritten in upgrades in future upgrade notes :)!

Jason Bert