Monday, June 25, 2012

ASP.NET Web API 4 Beta – Custom Unity Http Controller Factory

The ASP.NET Web API provides support for Restful services that make use of JSON and the concepts of ASP.NET MVC. Instead of the Controller base class in MVC the Web API uses the ApiController class. In order to use a dependency injection container like Unity for controller creation you need to create your own controller factory. This blog post shows how to build a custom Http controller factory together with the Unity application block.
A custom controller factory
Each controller factory in ASP.NET Web API must implement the IHttpControllerFactory interface. This again is different from MVC where you have to implement IControllerFactory. The IHttpControllerFactory provides just two methods – CreateController and ReleaseController. I think by reading the names it’s obvious what both methods should do. Together with Unity we will use these methods to create and release controllers with the help of the Unity container:
public class UnityControllerFactory : IHttpControllerFactory
{
    private readonly IUnityContainer container;
    private readonly IHttpControllerFactory defaultFactory;
    private readonly HttpConfiguration configuration;
    public UnityControllerFactory(IUnityContainer container,
HttpConfiguration configuration)
    {
        this.configuration = configuration;
        this.container = container;
        this.defaultFactory = new DefaultHttpControllerFactory(configuration);
    } 

    public IHttpController CreateController(
        HttpControllerContext controllerContext, string controllerName)
    {
        if (container.IsRegistered<IHttpController>(controllerName))
        {
            var controller = container.Resolve<IHttpController>(controllerName);
            controllerContext.ControllerDescriptor =
                new HttpControllerDescriptor(
this.configuration,
controllerName,
controller.GetType());
            controllerContext.Controller = controller;
            return controller;
        } 
        return defaultFactory.CreateController(controllerContext, controllerName);
    } 

    public void ReleaseController(IHttpController controller)
    {
        if (container.IsRegistered(controller.GetType()))
        {
            container.Teardown(controller);
        }
        else
        {
            defaultFactory.ReleaseController(controller);
        }
    }
} 
Let’s have a look at the code. We hold a reference to the Unity container in order to resolve controller dependencies. We also define a default factory in case a controller is not registered with unity. The framework provides the DefaultHttpControllerFactory which is normally used to create API controller. Another reference we need is to the current Http configuration. The configuration is needed to create a proper descriptor for the created controller.
The CreateController method tries to find the controller with the given name inside the Unity container. If the controller is not registered with Unity the default factory is used to create an instance of the controller. If the controller is registered it is resolved by Unity and a proper controller descriptor is generated. The descriptor together with the Http configuration is needed to build up a correct Http context in which the controller is executed.
The ReleaseController method again checks if the controller is registered with Unity or not and calls the appropriate release method.
Initializing the controller factory
If you have the custom Http controller factory there’s only one last step to use it. You need to instantiate the Unity container inside the global.asax and pass it to the factory. Additionally you need to set the factory as the default factory for creating API controllers. Here’s the short code snippet:
protected void Application_Start()
{
     
    var container = new UnityContainer();
    //register the customer controller with Unity
    container.RegisterType<IHttpController,
Controllers.CustomerController>("customer"); 
    //Add additional controllers here 
    var factory = new UnityControllerFactory(container,
GlobalConfiguration.Configuration);
    GlobalConfiguration.Configuration.ServiceResolver.SetService(
        typeof(IHttpControllerFactory), factory);
   
}
I think the code is more or less self-explanatory. By using the static Configuration property of the GlobalConfiguration  class you get access to the current Http configuration object, which is needed for the factory to build the controller descriptor. Pay attention to the controller name passed to the RegisterType method. This name should resemble the name of the controller used by API calls because that name is passed to the controller factory.

1 comment: