如何使用InterceptAttribute使用Ninject拦截



我有一个如下的NinjectWebCommon。我无法让TimingInterceptor触发具有"Timing"属性集的方法。如果拦截器定义在类级别(所有方法调用都将被拦截),则可以正常工作,但我希望能够指定我想要拦截的方法(选择加入)。

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

my Service类定义如下

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

您需要它是虚拟的,以便ject拦截它:

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

相关内容

  • 没有找到相关文章

最新更新