Applying Policy Injection on ApiController in WebApi

Topics: Enterprise Library Core, Policy Injection Application Block
Feb 27, 2014 at 3:53 PM

Can any body suggest me the approach to apply policy injection on apicontroller in webapi. Any link or sample will be appreciable. I did lot of googling & implemented InterfaceIntercept. It is working fine with other classes but not working with apicontroller (DI->unity).
Feb 28, 2014 at 10:14 AM
What version of Unity are you using?

Randy Levy
Enterprise Library support engineer
Support How-to
Feb 28, 2014 at 11:33 AM
Unity 3, Although I have done above implementation by making a abstract base controller class & then used virtual method intercept (EntLib 6). Would you like to comment on this approach ?
Mar 3, 2014 at 8:54 AM
What version of Web API are you using? If using the latest version the preferred way would be to use the Unity bootstrapper for ASP.NET Web API. Then you would implement a DependencyResolver instead of a ControllerFactory.

I'm not 100% sure I understand how all the components of your application are fitting together. One thing I did see based on the posted registration code is that you need to configure policy injection. Instead of:
container.Configure<Interception>().SetInterceptorFor<IDDNPublicAPI.PassThrough.IRequestExecutionController>(new InterfaceInterceptor()); 
You could try:
container.RegisterType<IRequestExecutionController, RequestExecutionController>(
    new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<PolicyInjectionBehavior>());

Although VirtualMethodInterceptor might be better if the runtime is requesting a concrete controller type (as opposed to an interface).

In general, you want to configure Dependency Injection using Unity with Web API. Once that is wired up (the NuGet package sets up all the hooks) then it is just a matter of configuring the policies; this might be just adding a few lines of registration code depending on the wiring approach chosen (e.g. programmatic matching rules, attributes, xml configuration).

You also might want to look at MVC/Web API ActionFilters to see if they can help achieve the functionality you are looking for.

Randy Levy
Enterprise Library support engineer
Support How-to