Implementing IHttpControllerFactory.CreateController

Topics: Web Api
Feb 17, 2012 at 12:44 PM

I am looking at the ASP.NET MVC 4 Beta version of Web API.

What is the proper approach to implementing IHttpControllerFactory.CreateController(HttpControllerContext controllerContext, string controllerName)? The DefaultHttpControllerFactory has two interesting behaviours in addition to resolving the Type from the controllerName:

1. It creates an HttpControllerDescriptor and attaches it to the HttpControllerContext. If you don't do this bad things happen!

2. To create the actual instance, it calls the registered IHttpControllerActivator passing the HttpControllerContext and calls its Create method passing through the HttpControllerContext and whatever Type it has resolved. The DefaultHttpControllerActivator ends up using the DependencyResolver.GetService to get the actual instance.

From this, should we surmise that IHttpControllerFactory is really more of a controller name -> Type resolver? Is it preferred that custom factories call whatever IHttpControllerActivator is registered or go their own way?

(I've posted this on ASP.NET as well, I will cross-post replies back - I'm not really sure what site to use now!)

Feb 17, 2012 at 2:11 PM

Just read Dan's announce post which clearly indicates posts should go to the ASP.NET Forum - so please refer to the cross-post here (which also includes my crummy implementation! :o )

http://forums.asp.net/t/1770736.aspx/1?Implementing+IHttpControllerFactory+CreateController