The Siege Project: Siege.ServiceLocation, Part 4 – Integrating Siege.ServiceLocation with ASP.NET MVC

The Siege Project


Get the library used to integrate with ASP.NET MVC by downloading the binaries here:
Alternatively, you can get the source code here:


Combining Siege.ServiceLocation and ASP.NET MVC

By combining Siege.ServiceLocation with ASP.NET MVC, everything starts to come together. You get more than just the ability to abstract away your dependency on an individual IoC framework (and thereby allowing you to change implementations at the drop of a hat), you get the ability to react to changing conditions in your application and the individual user sessions. When resolving controllers, you have the ability to use views linked to either the contextually resolved controller, or linked to the base type that was requested. Integrating these two frameworks, it turns out, is incredibly simple to do.



SiegeHttpApplication is the main component that enables you to integrate with ASP.NET MVC. Inherit your Global application in the Global.asax.cs file from this class in order to turn your regular ASP.NET MVC application into a Siege enabled application. This abstract class forces you to implement only a handful of methods and offers you the ability to override several others (not actual implementation).

public abstract class SiegeHttpApplication : HttpApplication


    public IContextualServiceLocator ServiceLocator;


    protected abstract IServiceLocatorAdapter GetServiceLocatorAdapter();


    public virtual void RegisterRoutes(RouteCollection routes) { }


    protected virtual IContextStore GetContextStore()




    protected virtual void OnApplicationStarted()




    protected virtual void OnApplicationStopped()




    protected virtual void OnApplicationError(Exception exception)




The only method you must implement is GetServiceLocatorAdapter(). This instructs the SiegeHttpApplication what underlying IoC framework you wish to use. Other than this method, you have the ability to override the default IContextStore used by the SiegeContainer (by default the HttpSessionStore is used). OnApplicationStarted, OnApplicationStopped and OnApplicationError give you extension points to provide additional logic by which you can add custom logic for your own application.



By default, SiegeHttpApplication uses an implementation of IContextStore with the SiegeContainer called HttpSessionStore. This class stores items on the session of the user navigating your website, allowing you to control resolution on a user-by-user basis over the life of the user session. Developers can change this to any other implementation of IContextStore – GlobalContextStore to make your entire application change implementations based on context (for responding to global conditions) or to a custom implementation, if you need finer control over how context is stored and used in the resolution process.


And that’s it!

That’s all it takes to use Siege.ServiceLocation with ASP.NET MVC. I have put together a quick start application that shows in detail how all these parts work together.

Happy Coding!

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

This entry was posted in IoC, MVC, Siege. Bookmark the permalink. Follow any comments here with the RSS feed for this post.