策略注入拦截不适用于Web API控制器

时间:2014-02-27 20:46:30

标签: c# dependency-injection asp.net-web-api enterprise-library policy-injection

我正在使用web api实现Policy injection,而对于DI,我们正在使用自定义DependancyResolver。我使用InterfaceIntercept方法来实现策略注入。它在Classes(自定义创建的类)的情况下工作正常,但在ApiController的情况下不会触发Policy Injection。

要使用API​​Controller调用Policy Injection,我创建了一个接口&用控制器实现了这一点。分享以下代码: 此外,我还需要使用MessageHandler调用策略。

政策注入代码:

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; }
}

属性代码

[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();
    }
}

接口代码:此接口由ApiController实现

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

IRequestExecutionController接口由RequestExecutionController实现。 统一注册类型:

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

注册拦截

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

由于我们有统一来解决依赖关系,所以我们创建了一个控制器工厂类来处理Controller实例创建。

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)
    {`enter code here`
        this.container = container;
        this.innerFactory = innerFactory;
    }enter code here

    public IHttpController Create(HttpRequestMessa`enter code here`ge request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    {       
        var controller = (IHttpController)this.container.Resolve(controllerType, controllerDescriptor.ControllerName.ToLower());          
        return controller;
    }
} 

我们已在全局配置文件中注册此Controller工厂。相同的过程适用于其他类,但不适用于apicontroller。

任何人都可以对此提出一些建议吗?

1 个答案:

答案 0 :(得分:0)

Web API控制器无法拦截。但是你可以使用过滤器获得相同的结果。

这是一篇很好的帖子,展示了如何使用过滤器在控制器中进行日志记录: http://damienbod.wordpress.com/2013/09/15/aop-with-asp-net-web-api/

您仍然可以使用日志记录拦截器记录任何后端代码。