Policy Injection Intercept not working for WebAPI Controllers

Topics: Logging Application Block, Policy Injection Application Block
Feb 27, 2014 at 12:18 PM
Hi,

I'm implementing Policy injection with web api and for DI we are using custom DependancyResolver. I have used InterfaceIntercept approach to implement policy injection. It is working fine in case of Classes(custom created classes) but Policy Injection is not being fired in case of ApiController.

To call Policy Injection with APIController, I have created an interface & implemented that with the controller. Sharing code below :
Also I would need to call policy with MessageHandlers as well.

Policy Injection code :

public class LogExceptionsCallHandler : ICallHandler
{

    public IMethodReturn Invoke(IMethodInvocation input,
                                GetNextHandlerDelegate getNext)
    {
        IApplicationLogger logger = new ApplicationLogger();
        logger.Log(LoggingLevel.TRACE, "Entering " + input.MethodBase.DeclaringType.FullName + "." + input.MethodBase.Name);
        //// Perform the operation
        var methodReturn = getNext().Invoke(input, getNext);
        //// Method failed, go ahead
        if (methodReturn.Exception != null)
            return methodReturn;
        //// If the result is negative, then throw an exception
        logger.Log(LoggingLevel.TRACE, "Ending " + input.MethodBase.DeclaringType.FullName + "." + input.MethodBase.Name);

        return methodReturn;
    }
    public int Order { get; set; }
}

Attribute code

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Method)]
public class LogExceptionsAttribute : HandlerAttribute
{
    public LogExceptionsAttribute()
    {
    }
    public HandleLogging HandleLogging { get; set; }
    public int RetryCount { get; set; }
    public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container)
    {
        return new LogExceptionsCallHandler();
    }
}

Interface code :This interface is being implemented by the ApiController

[LogExceptions]
public interface IRequestExecutionController : IHttpController
{
    [LogExceptions]
    HttpResponseMessage ExecuteRequest();
}

IRequestExecutionController interface is being implemented by the RequestExecutionController.

Registering type with unity :

container.RegisterType<IDDNPublicAPI.PassThrough.IRequestExecutionController, RequestExecutionController>("requestexecution");

Registering Intercept

container.Configure<Interception>().SetInterceptorFor<IDDNPublicAPI.PassThrough.IRequestExecutionController>(new InterfaceInterceptor());


As we have unity to resolve dependency, So we have created a controller factory class to handle Controller instance creation.
public class UnityControllerFactory : IHttpControllerActivator
{
    private IUnityContainer container;

    private readonly IControllerFactory innerFactory;

    public UnityControllerFactory(IUnityContainer container)
        : this(container, new DefaultControllerFactory())
    { }

    public UnityControllerFactory(IUnityContainer container, IControllerFactory innerFactory)
    {
        this.container = container;
        this.innerFactory = innerFactory;
    }

    public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    {       
        var controller = (IHttpController)this.container.Resolve(controllerType, controllerDescriptor.ControllerName.ToLower());          
        return controller;
    }
} 
And we have register this Controller factory in global config file. Same process is working for other classes but not working for the apicontroller.


Can any one suggest something on this ?