2013-03-19 6 views
5

次のようにNinjectWebCommonがあります。 TimingInterceptorを "Timing"属性が設定されているメソッドでトリガーすることができません。すべてのメソッド呼び出しがインターセプトされるクラスレベルでインターセプターが定義されている場合はうまく動作しますが、インターセプトする(オプトインする)メソッドを指定することができます。InterceptAttributeを使用してNinjectインターセプトを使用する方法

私はNinject.Extensions.Interception.DynamicProxyを持っていますが追加されました。

public static class NinjectWebCommon 
{ 
    private static readonly Bootstrapper bootstrapper = new Bootstrapper(); 

    /// <summary> 
    /// Starts the application 
    /// </summary> 
    public static void Start() 
    { 
     DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); 
     DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); 
     bootstrapper.Initialize(CreateKernel); 
    } 

    /// <summary> 
    /// Stops the application. 
    /// </summary> 
    public static void Stop() 
    { 
     bootstrapper.ShutDown(); 
    } 

    /// <summary> 
    /// Creates the kernel that will manage your application. 
    /// </summary> 
    /// <returns>The created kernel.</returns> 
    private static IKernel CreateKernel() 
    { 
     var NinjectSettings = new NinjectSettings(); 
     var kernel = new StandardKernel(NinjectSettings); 

     kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
     kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

     GlobalConfiguration.Configuration.DependencyResolver = new BazingaNinjectResolver(kernel); 

     RegisterServices(kernel); 
     return kernel; 
    } 

    /// <summary> 
    /// Load your modules or register your services here! 
    /// </summary> 
    /// <param name="kernel">The kernel.</param> 
    private static void RegisterServices(IKernel kernel) 
    { 
     kernel.Bind<IMyService>().To<MyService>().InRequestScope(); 
    }   
} 

私のサービスクラスが

public class MyService : IMyService 
{ 
    Logger log; 

    public MyService() 
    { 
     log = LogManager.GetLogger(this.GetType().FullName); 
    } 

    [Timing] 
    public string GetString() 
    { 
     log.Info("log me!!"); 
     return "Cool string !!!!"; 
    } 

    public string GetUnInterceptString() 
    { 
     return "Not intercepted"; 
    } 
} 

インターセプタを追跡し、あなたがそれを傍受するninjectため、仮想する必要が

public class TimingAttribute : InterceptAttribute 
{ 
    public override IInterceptor CreateInterceptor(IProxyRequest request) 
    { 
     return request.Context.Kernel.Get<TimingInterceptor>(); 
    } 
} 

public class TimingInterceptor : SimpleInterceptor 
{ 
    readonly Stopwatch _stopwatch = new Stopwatch(); 

    protected override void BeforeInvoke(IInvocation invocation) 
    { 
     _stopwatch.Start(); 
    } 

    protected override void AfterInvoke(IInvocation invocation) 
    { 
     _stopwatch.Stop(); 
     string message = string.Format("Execution of {0} took {1}.", 
      invocation.Request.Method, 
      _stopwatch.Elapsed); 

     _stopwatch.Reset(); 
    } 
} 

答えて

3

を次のように定義する属性として定義します。

public class MyService : IMyService 
{ 
    Logger log; 

    public MyService() 
    { 
     log = LogManager.GetLogger(this.GetType().FullName); 
    } 

    [Timing] 
    public virtual string GetString() 
    { 
     log.Info("log me!!"); 
     return "Cool string !!!!"; 
    } 

    public string GetUnInterceptString() 
    { 
     return "Not intercepted"; 
    } 
} 
+0

私はそれがどこかで言及されているのを見て覚えていました。今度はおかげで – Eatdoku

+0

属性を使ってプライベートメソッドを傍受することは可能ですか? – Eatdoku

+2

鼻血ではなく、ポストシャープ、または他のAOP魔法が必要です –

関連する問題