开发者

Ninject: Method not getting intercepted in Silverlight

开发者 https://www.devze.com 2023-01-06 14:10 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消