Ninject:方法未在 Silverlight 中被拦截

发布于 2024-09-08 22:08:52 字数 2512 浏览 7 评论 0原文

我在 Silverlight 应用程序中使用 Ninject 进行 DI。现在我正在尝试实施拦截并遇到问题。我的方法没有被拦截。下面是

public class InfrastructureModule : NinjectModule
{
    public override void Load()
    {
        Bind<IGlobalEventManager>().To<GlobalEventManager>().InSingletonScope();
        Bind<IViewModel>().To<ViewModel>().InSingletonScope();
    }
}

App.xaml 中的示例实现 --> Application_Startup 事件,我执行绑定并调用我期望被拦截的方法:

private void Application_Startup(object sender, StartupEventArgs e)
{
    IKernel kernel = new StandardKernel(new InfrastructureModule());

    IViewModel vm = kernel.Get<IViewModel>();
    vm.SomeMethod();

    this.RootVisual = new MainPage();
}

ViewModel 的实现:

public class ViewModel : IViewModel
{
    [LockUI]
    public virtual void SomeMethod()
    {
        MessageBox.Show("Hello");
    }
}

LockUI 属性:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false,
    Inherited = true)]
public class LockUIAttribute : InterceptAttribute
{
    public override IInterceptor CreateInterceptor(IProxyRequest request)
    {
        return request.Context.Kernel.Get<LockUIInterceptor>();
    }
}

LockUIInterceptor 实现:

public class LockUIInterceptor : SimpleInterceptor
{
    private IGlobalEventManager _globalEventManager;

    public LockUIInterceptor(IGlobalEventManager globalEventManager)
    {
        _globalEventManager = globalEventManager;
    }

    protected override void BeforeInvoke(IInvocation invocation)
    {
        _globalEventManager.OnLockUI(this, EventArgs.Empty);
    }
}

GlobalEventManager 实现:

public class GlobalEventManager : IGlobalEventManager
{
    private static int _asyncCallCount;

    public event EventHandler UnLockUI;

    public event EventHandler LockUI;

    public void OnLockUI(object sender, EventArgs e)
    {
        if (LockUI != null)
        {
            _asyncCallCount += 1;
            LockUI(sender, e);
        }
    }

    public void OnUnlockUI(object sender, EventArgs e)
    {
        if (UnLockUI != null)
        {
            _asyncCallCount -= 1;

            if (_asyncCallCount == 0)
                UnLockUI(sender, e);
        }
    }
}

我添加了对 Castle.Core、Castle.DynamicProxy2、Ninject、Ninject.Extensions.Interception 的引用, Ninject.Extensions.Interception.DynamicProxy2

有人可以帮忙吗?谢谢

I use Ninject for DI in Silverlight app. Now I am trying to implement interception and having issue. My methhod is not getting intercepted. Below is sample implementation

public class InfrastructureModule : NinjectModule
{
    public override void Load()
    {
        Bind<IGlobalEventManager>().To<GlobalEventManager>().InSingletonScope();
        Bind<IViewModel>().To<ViewModel>().InSingletonScope();
    }
}

In App.xaml --> Application_Startup event, i do the binding and call method i expect to be intercepted:

private void Application_Startup(object sender, StartupEventArgs e)
{
    IKernel kernel = new StandardKernel(new InfrastructureModule());

    IViewModel vm = kernel.Get<IViewModel>();
    vm.SomeMethod();

    this.RootVisual = new MainPage();
}

Implementation of ViewModel:

public class ViewModel : IViewModel
{
    [LockUI]
    public virtual void SomeMethod()
    {
        MessageBox.Show("Hello");
    }
}

LockUI attribute:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false,
    Inherited = true)]
public class LockUIAttribute : InterceptAttribute
{
    public override IInterceptor CreateInterceptor(IProxyRequest request)
    {
        return request.Context.Kernel.Get<LockUIInterceptor>();
    }
}

LockUIInterceptor implementation:

public class LockUIInterceptor : SimpleInterceptor
{
    private IGlobalEventManager _globalEventManager;

    public LockUIInterceptor(IGlobalEventManager globalEventManager)
    {
        _globalEventManager = globalEventManager;
    }

    protected override void BeforeInvoke(IInvocation invocation)
    {
        _globalEventManager.OnLockUI(this, EventArgs.Empty);
    }
}

GlobalEventManager implementation:

public class GlobalEventManager : IGlobalEventManager
{
    private static int _asyncCallCount;

    public event EventHandler UnLockUI;

    public event EventHandler LockUI;

    public void OnLockUI(object sender, EventArgs e)
    {
        if (LockUI != null)
        {
            _asyncCallCount += 1;
            LockUI(sender, e);
        }
    }

    public void OnUnlockUI(object sender, EventArgs e)
    {
        if (UnLockUI != null)
        {
            _asyncCallCount -= 1;

            if (_asyncCallCount == 0)
                UnLockUI(sender, e);
        }
    }
}

I have added reference to Castle.Core, Castle.DynamicProxy2, Ninject, Ninject.Extensions.Interception, Ninject.Extensions.Interception.DynamicProxy2

Could someone please help. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文