MVC and Unity

Nov 6, 2008 at 9:45 AM
Hi All

I found many samples contains following code for IControllerfactory. But it seems the interface for the CreateController is not same now. How could I revise the following code to support MVC?

public class CustomControllerFactory : IControllerFactory
public IController CreateController
(RequestContext context, Type controllerType)
IContainerAccessor containerAccessor =
as IContainerAccessor;


return containerAccessor.Container.Get(controllerType)
as IController;

P.S. Current interface for CreateController is as follows:-
   public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)


Nov 6, 2008 at 10:28 PM
Here's an example controller factory that calls Unity:

    public class UnityControllerFactory : DefaultControllerFactory
        private readonly IUnityContainer container;
        public UnityControllerFactory(IUnityContainer container)
            this.container = container;

        protected override IController GetControllerInstance(Type controllerType)
            return (IController) container.Resolve(controllerType);

Nov 7, 2008 at 1:02 AM
Thanks. However, if I use this approach, is it possible to use  the Policy Injection with the controller since no interface is exposed?

Nov 7, 2008 at 4:42 AM
You need to choose which interceptor you want to use, and create your controller classes in a way that conforms to the requirements of that interceptor.

Since you could never have an interface exposed through here anyway, your choices are the TransparentProxyInterceptor and the VirtualMethodInterceptor. The TransparentProxy's no good, because ControllerBase doesn't derive from MarshalByRefObject (I'll see if I can get that changed :-)). So, to get interception working on controllers, make all your action methods virtual, and use the VirtualMethodInterceptor.

Nov 7, 2008 at 8:41 AM
Thanks, Is it support namespace matching?

I found it works now but only use the attribute as follows.
[ExceptionCallHandler("Exception Policy")]
Nov 7, 2008 at 4:40 PM
All the shipped matching rules should work with all interceptors. Could you please post a small sample that reproduces the issue you've run into?
Nov 10, 2008 at 7:32 AM
Here is the sample code for the IAccoutnManger I created. It won't work with the Matching rule under the web.config.

           .RegisterType<IFormsAuthentication, FormsAuthenticationWrapper>(new ContainerControlledLifetimeManager())
           .RegisterType<IAccountManager, AccountManager>(new ContainerControlledLifetimeManager())

            _container.Configure<Interception>().SetInterceptorFor<IAccountManager>(new TransparentProxyInterceptor());

And I have registered 

BTW, I try to separate the interface and the implementation.
Hence, I have one web proejct and two DLLs. One is for Account Manger and ther other is IAccoutnManager.

Nov 10, 2008 at 4:52 PM
What does the configuration for the matching rules looks like? Did you use the PIAB configuration or the Unity interception configuration?

What namespaces are your classes in?

It's rather hard to debug the interception setup when that's the piece of information you didn't provide.


Nov 11, 2008 at 3:32 AM
Thanks Chris

When I want to share my web.config, I found that the namespace collection haven't been modified through the Enterprise Library Editing tools.

Now, I modify it manually and it works now.

Thanks again Chris.