如何根据请求在Windsor中获取HttpContext

时间:2017-05-03 11:57:24

标签: c# castle-windsor

我正在使用Windsor Castle实施审计经理。但是,我不确定如何根据请求获取当前正在执行的HttpContext。我需要它来记录请求中的某些信息,例如I.P.地址。如果可能的话,你能不能告诉我一个安全的&将返回值转换为JsonResult对象的简单方法,如果是那种类型?

public class ControllersInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(
            Classes.FromThisAssembly()
            .BasedOn<IController>()
            // a new instance should be provided by Windsor every time it is needed
            .LifestyleTransient()
            .Configure(c => c.Interceptors(new InterceptorReference(typeof(ControllerInterceptor)))));
    }

    public class ControllerInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            try
            {
                string controller = invocation.TargetType.FullName;
                string method = invocation.Method.Name;
                List<string> parameters = new List<string>();

                for (int i = 0; i < invocation.Arguments.Length; i++)
                {
                    var param = invocation.Arguments[i];
                    parameters.Add(param.ToString());
                }

                invocation.Proceed();

                string result = JsonConvert.SerializeObject(invocation.ReturnValue ?? new object());

                var auditItem = new AuditItem
                {
                    ActionRequested = method,
                    Controller = controller,
                    Denied = false,
                };

            }
            catch (Exception ex)
            {
                _log.Error("ControllerInterceptor.Intercept:: " + ex.Message, ex);
            }
        }
    }

0 个答案:

没有答案