Fork me on GitHub
AutoBox - On the fly Dependency Injection and caching container autobox
AutoBox is a on the fly dependency injection and caching container. It dynamically injects dependencies based on convention. Also, let you cache repository calls to memcached (the most popular cross-platform caching system).
PM > Install-Package AutoBox

Gettting Started

Let us assume that you have a controller and the constructor looks like this:

public AccountController(IAccountService service)
	this.service = service;

Here AccountService is implemented in MyCoolWebSite.Services namespace, in global.ascx you first need to add the following line:


However it is possible to specify assembly explictly. For example:


Similarly you can do:


Here to mention that AutoBox will do the mapping for a service type (interface) on demand thus making it lazy.

Next you need to create a controller factory from DefaultControllerFactory that overrides GetControllerInstance. Since AutoBox is implemented using CommonServiceLocator you can directly include ServiceLocator.Current.GetInstance that will return the target controller with depencies properly injected.

protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        return ServiceLocator.Current.GetInstance(controllerType) as IController;

Finally, set your controller factory as current with the following line:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

Now, moving forward there is a method in IProductRepository lets say IProductReposiroty.GetAllProducts(). You just dont want to hit database all the time, unless invalidated. Therefore you can further specify:

Container.Setup<ProductRepository>(x=> x.GetAllProducts()).Caches(TimeSpan.FromMinutes(10));

This tells AutoBox to cache the result of the call that will automatically invalidate after ten minutes. Moreover you can explictly specify the method that will invalidate it:

Container.Setup<ProductRepository>(x=> x.Create(Arg.Varies<Product>()).Invalidates(x => x.GetAllProducts());

Arg.Varies specifies that the repository call will be cached/invalidated for variable/dynamic arguments, this is very much to the output cache VaryByParams option.However to cache an repository call, instead of Arg.Varies if you just want to pass default values and want to specify varible arguments option fluently, you can always do:

Container.Setup<ProductRepository>(x=> x.GetPrdocutById(0)).Caches(TimeSpan.FromMinutes(10)).VaryByArgs();

As stated above by default, caching is done using memcached therefore to make memcached working you will need to include the following block in web.config (will be added automatically when installed from nuget).

<?xml version="1.0" encoding="utf-8"?>

			<section name="autoBox" type="AutoBox.AutoBoxSection, AutoBox" />

	<autoBox cacheStore="localhost" cacheProvider="MemcachedProvider" />


Here you will notice that by default its pointing to localhost. However it can be an external IP pointing to Amazon ElastiCache(released a few weeks ago).Therefore its safe to say that memcached is a caching machanism and the tool is agnostic of any underlying cloud service that you may choose.

To test caching locally, you can use the CouchBase Membase server (Not limited to), it gives you a nice web based GUI to monitor the cache usage, configure memory and add clusters.

You can download it from this link:

Once installed, double-click the “Membase Console” from desktop which will take you through an easy wizard to configure memcached.

Moving forward, there is a AutoBox.Specification project. You can use TestDriven.Net to verify that all tests pass and you have filled the gaps correctly.Lastly, to raise any question or file issues, please use the github project url (can be reached using the fork link above(top-right)).

What is memcached ?

Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

you will find more information on this at :


If dont want to use membase server (which is pretty easy) then you can also try the command-line tool from jellycan.

There is a nice document on getting started with Amazon ElastiCache:

Getting started with Amazon ElastiCache

Blog Posts

Writing ASP.NET MVC bootstrapper using AutoBox

Known issues

VerificationException -> Operation could destablize the runtime

This happens due to the conflict between NewtonSoft.dll and Intellitrace.Try to add NewtonSoft.dll to intellitrace ignore list.

However, you can also turn off the intellitrace to avoid this.


Configuring Membase server

Since membase uses IP_Addr.bat from %Program_Files%\Membase\Server\bin that dynamically resolves the IP for your machine. In case that you are under a router and your IP is assigned dynamically each time, you can try the following:

Update the follwoing line in service_register.bat:


Once you are done, you can run service_register.bat(from command line or just double click it) that will initalize and register the membase server as a local service with your specified IP.

Moreover, you might find that membase server is not starting if you dont have VC++ redist installed. In that regard please check the following thread:

Revision History

v1.0.1 : Initial release

v1.0.2 : Exception for null controller type (Skip resolve)

v1.0.3 : Included fluent VaryByArgs for Caches method

v1.0.4 : Arg.Varies fix for Invalidating cache.

v1.0.5 : Support of ServiceLocator.Current.GetAllInstances{T}

v1.1.2 : Dependency processing by Castle.Windsor and nested dependency resolve bug fix.