2013-03-19 98 views
5

我有一個NinjectWebCommon如下。我無法讓TimingInterceptor在具有「Timing」屬性集的方法上觸發。如果intercetor是在所有方法調用將被攔截的類級定義的,但是我希望能夠指定我想攔截的方法(可選),那麼它可以正常工作。如何使用Ninject攔截使用InterceptAttribute

我確實有添加了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"; 
    } 
} 

攔截和屬性定義如下

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

你需要它是虛擬的,ninject攔截它:

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 2013-03-19 16:39:12

+0

是否有可能使用屬性攔截私人方法? – Eatdoku 2013-03-19 16:44:27

+2

不與ninject,你需要postsharp,或一些其他AOP魔術 – 2013-03-19 16:54:30