MVC with PolicyInjection and Exception Handling and Unity

Topics: Building and extending application blocks, Enterprise Library Core, Exception Handling Application Block, General discussion, Policy Injection Application Block
Nov 4, 2008 at 3:03 AM
Hi All

I am using MVC beta version now with Unity.

What I would like to enhance is to merge it with the PolicyInjection with Exception handling. I have found some document could wrap the object you created for injection.

But if the exception occurred under Controller, could I still use this approach? Please advise.

Thanks and Regards

Nov 4, 2008 at 12:15 PM
Hi Alex,

What do you mean by occurred under controller? Do you mean that the exception was thrown within an action of a controller?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
Nov 4, 2008 at 2:04 PM
HI Valiant

Could I implement an error trap on this with Policy Injection and Unity?

Nov 4, 2008 at 7:22 PM
Yes. You'll need to use a custom ControllerFactory so that the MVC framework creates the controllers through Unity. From there, you can use interception against your controllers just like you can on any other class.
Nov 5, 2008 at 2:09 AM
I found that there are two solution to create a custom controllerFactory in the Web.

One is inherit DefaultControllerFactory and the other is using the IControllerFactory.

Which one is more appropriate?
Nov 5, 2008 at 2:47 AM

Please have a look on this link: It has a part that discuss a bit about creating custom controller factory.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
Nov 5, 2008 at 10:38 AM


But how do I intercept the custom Controller?

           .RegisterType<IFormsAuthentication, FormsAuthenticationWrapper>(new ContainerControlledLifetimeManager());

         _container.Configure<Interception>().SetInterceptorFor<CustomController>(new TransparentProxyInterceptor()); <--This doesn't work and following error occurred.

Exception Details: System.Web.HttpException: The incoming request does not match any route.

BTW, any full samples in the Web for this?



Nov 6, 2008 at 2:39 AM

You can try this links.

Sample App

Web Cast

Other Resources

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.

Nov 6, 2008 at 4:30 AM

But there is no help on how to intercept the Controller under MVC with Unity.

Any examples could be provided for this?

Nov 6, 2008 at 6:05 AM
Hi Alex,

I cant seem to find any sample with regards to your problem. Btw, I have a look at your exception and i've repro it when i remove the 

RegisterRoutes(RouteTable.Routes); inside the Application_Start in my Global.asax.cs, it throws the same exception.

This code seems to work:



protected void Application_Start()







UnityContainer _container = new UnityContainer();









IFormsAuthentication, FormsAuthenticationWrapper>(new ContainerControlledLifetimeManager())



IControllerFactory, MyCustomController>();



Interception>().SetInterceptorFor<IControllerFactory>(new TransparentProxyInterceptor());



var factory = (IControllerFactory)_container.Resolve<IControllerFactory>();



IController controller = factory.CreateController(Foo, "Foo");




also, i think you can post this to the Unity threads.


Nov 14, 2008 at 5:01 PM
You cannot (currently) use the TransparentProxyInterceptor with MVC controllers, because there's no useful controller interface, and the base class doesn't derive from MarshalByRefObject.

(I guess you could try to intercept IController.Execute, but that's a little too broad for my tastes).

If you make all your controller action methods virtual, you could use the VirtualMethodInterceptor instead.

I'm trying to get the MVC team to change that base class so that the TransparentProxyInterceptor will work. Discussions are ongoing.