2013-03-07 57 views
1

我想在動態代理攔截器方法中找到控制器和動作名稱 我檢查堆棧跟蹤接近不好的方式beacase它不是最後堆棧 這是我的代碼獲取方法城堡windsor攔截器方法中的來電者(控制器名稱和動作名稱)

全球ASAX城堡配置

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

攔截器類

//i want to find Controller name 

public class MyInterceptor : IInterceptor 
{ 
    public void Intercept(IInvocation invocation) 
    { 
     //?? controller name ?? method Name 
     invocation.Proceed(); 
    } 
} 

回答

0

DynamicProxy不公開來電信息。

0

我能夠使用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); 
     } 
    } 

} 
+0

感謝你在我loggingInterceptor

獲取類名和方法名,但我想主叫類名稱,而不是調用的類和方法方法 !我使用企業庫實例的城堡並正常工作 – ARA 2013-05-06 15:13:36

相關問題