2013-03-19 6 views
5

Ho un NinjectWebCommon come segue. Non riesco a far attivare il TimingInterceptor sul metodo con l'attributo "Temporizzazione" impostato. Funziona bene se l'intercetore è definito a livello di classe in cui verrà intercettata tutta la chiamata al metodo, ma mi piacerebbe avere la possibilità di specificare il metodo che voglio intercettare (opt in).come utilizzare l'intercettazione di Ninject usando InterceptAttribute

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

mia classe di servizio definiscono seguire

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"; 
    } 
} 

Interceptor e attributi definiscono come segue

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

risposta

3

È necessario che sia virtuale, per Ninject per intercettarla:

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

giusto, mi sono ricordato di averlo visto da qualche parte, funziona ora grazie – Eatdoku

+0

è possibile intercettare metodi privati ​​usando l'attributo? – Eatdoku

+2

Non con ninject, avresti bisogno di postsharp, o qualche altra magia AOP –

Problemi correlati