c# - 如何使用 InterceptAttribute 使用 Ninject 拦截

标签 c# ninject aop ninject-extensions ninject-interception

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

我的服务类定义如下

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

最佳答案

你需要它是虚拟的,以便 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";
    }
}

关于c# - 如何使用 InterceptAttribute 使用 Ninject 拦截,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505051/

相关文章:

java - AspectJ @DeclareParents defaultImpl 代码在用作依赖项时不使用

c# - 使用 NEST C# 的 ElasticSearch 2.x GeoPoint 映射

C# 获取 XML 标记值

c# - 在 azure 中发布网站时,未发布已编辑的页面

c# - DI Framework : how to avoid continually passing injected dependencies up the chain, 且未使用服务定位器(特别是使用 Ninject)

java - Spring 框架中的切入点在内部如何工作

c# - 从 Gridview RowDeleting 事件调用 Javascript 函数

asp.net-mvc-3 - 如何将 nhibernate 事务添加到 ninject?

Ninject - 如何从注入(inject)的子对象引用父对象

java - 你会使用 AOP 来跟踪积分吗?