在castle windsor interceptor方法中获取方法调用者(Controller Name和Action

本文关键字:方法 Controller Name Action 调用者 获取 castle windsor interceptor | 更新日期: 2023-09-27 18:02:49

我想在动态代理拦截器方法中找到控制器和动作名称我检查堆栈跟踪方法不是好方法,因为它不会持续在堆栈中这是我的代码

global asax castle config

IWindsorContainer ioc = new WindsorContainer();
ioc.Register(
Component.For<IMyService>().DependsOn()
.ImplementedBy<MyService>()
.Interceptors<MyInterceptor>()
.LifeStyle.PerWebRequest);
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(ioc));
ioc.Register(
Component.For<IInterceptor>()
.ImplementedBy<MyInterceptor>());
控制器类

private IMyService _service;
public HomeController(IMyService service)
{
    _service = service;
}
public ActionResult Index()
{
    _service.HelloWorld();
    return View();
}
服务类

public class MyService : IMyService
{
    public void HelloWorld()
    {
        throw new Exception("error");
    }
}
public interface IMyService
{
    void HelloWorld();
}
<<p> 拦截器类/strong>
//i want to find Controller name  
public class MyInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        //?? controller name ?? method Name  
        invocation.Proceed();
    }
}

在castle windsor interceptor方法中获取方法调用者(Controller Name和Action

DynamicProxy不公开调用者信息

我能够在我的loggingInterceptor中获得类名和方法名

使用<<p> em> invocation.TargetType.Name
public class LoggingInterceptor : IInterceptor
{ 
    ...
    public void Intercept(IInvocation invocation)
    {
        try
        {
            this.Logger.InfoFormat(
                "{0} | Entering method [{1}] with paramters: {2}",
                invocation.TargetType.Name,
                invocation.Method.Name,
                this.GetInvocationDetails(invocation));
            invocation.Proceed();
        }
        catch (Exception e)
        {
            this.Logger.ErrorFormat(
                "{0} | ...Logging an exception has occurred: {1}", invocation.TargetType.Name, e);
            throw;
        }
        finally
        {
            this.Logger.InfoFormat(
                "{0} | Leaving method [{1}] with return value {2}",
                invocation.TargetType.Name,
                invocation.Method.Name,
                invocation.ReturnValue);
        }
    } 
}