C#ExceptionFilterAttribute中的依赖注入

use*_*244 3 c# dependency-injection castle-windsor inversion-of-control asp.net-web-api

我在应用程序中使用Castle Windsor,我想在ExceptionFilterAttribute中使用注入一些服务示例ILog:

public class GenericExceptionFilterAttribute : ExceptionFilterAttribute
{
    private readonly ILog _logger;

    public GenericExceptionFilterAttribute()            
    {

    }

    public GenericExceptionFilterAttribute(ILogManager logManager)
    {
        _logger = logManager.GetLogger(typeof(GenericExceptionFilterAttribute));
    }
}
Run Code Online (Sandbox Code Playgroud)

我如何在该课程中注入服务?

问候

卡洛斯

use*_*244 5

嗨dependencyResolver来解决这个问题:

 public override void OnException(HttpActionExecutedContext context)
                {
                    var log= (ILog)context.ActionContext.ControllerContext.Configuration.DependencyResolver.GetService(typeof(ILog));    
                }
Run Code Online (Sandbox Code Playgroud)