2017-05-03 15 views
0

Windsor Castleを使用して監査マネージャを実装しています。しかし、要求ごとに現在実行中のHttpContextを取得する方法が不明です。 I.Pのようなリクエスト内の特定の情報を記録する必要があります。住所。可能であれば、戻り値をJsonResultオブジェクトに変換するための安全な方法がある場合は、&があるかどうか教えてください。リクエストごとにWindsor内でHttpContextを取得する方法

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

1)通常、インターセプタは、一時的なライフスタイルでコンテナに登録する必要があります。次に、コンポーネント登録で.Interceptors ()を使用します。 2)httpcontextがインターセプタターゲットのプロパティとして使用可能な場合、IInvocationのプロキシプロパティを使用してターゲットを見つけることができます。 – Marwijn

答えて

1

HttpContextBaseをCastle Windsorに登録して工場出荷時の方法で解決できます。ライフスタイルをPerWebRequestにすると、リクエストごとにスコープを設定できます。

container.Register(
    Component.For<HttpContextBase>() 
      .UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)) 
      .LifestylePerWebRequest()); 
関連する問題