You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have ran into an issue when binding two interfaces to the same concrete class when there is an Interceptor on the singleton binding.
Without the Interceptor these tests will all pass because when InterfaceA or InterfaceB is injected it is injected as the concrete class type. However with the interceptors it puts Castle.Proxies.InterfaceAProxy Or Castle.Proxies.InterfaceBProxy purely dependent upon which Kernel.Get was called first. Since InterfaceA inherits from InterfaceB, when InterfaceA goes first it can cast as the proxy for InterfaceB. But it will not work the other way around.
I have taken the complexity out of the instance I ran into and put it into a very basic scenario below. To run this snippet create a UnitTest Project in VisualStudio and add the Ninject.Extensions.Interception.DynamicProxy NuGet Package v3.3.3.
publicclassInterceptor:IInterceptor{publicvoidIntercept(IInvocationinvocation){//Interceptor Logic}}publicinterfaceInterfaceB{//Definition}publicinterfaceInterfaceA:InterfaceB{//Definition}publicclassImplementation:InterfaceA{//Logic}publicclassBusinessLogic_One{privatereadonlyInterfaceA_interface;publicBusinessLogic_One(InterfaceAinput){_interface=input;}}publicclassBusinessLogic_Two{privatereadonlyInterfaceB_interface;publicBusinessLogic_Two(InterfaceBinput){_interface=input;}}[TestClass]publicclassUnitTests{privateIKernel_kernel;[TestInitialize]publicvoidTestInit(){_kernel=new StandardKernel();//If the Intercept().With<Interceptor>() is commented out all tests will pass.
_kernel.Bind<InterfaceA,InterfaceB>().To<Implementation>().InSingletonScope().Intercept().With<Interceptor>();
_kernel.Bind<BusinessLogic_One>().ToSelf().InSingletonScope();
_kernel.Bind<BusinessLogic_Two>().ToSelf().InSingletonScope();}[TestMethod]//Test PassespublicvoidTest_Calling_KernelGet_InterfaceAFirst(){varbl1= _kernel.Get<BusinessLogic_One>();//InterfaceAvarbl2= _kernel.Get<BusinessLogic_Two>();//InterfaceB}[TestMethod]//Test FailspublicvoidTest_Calling_KernelGet_InterfaceBFirst(){varbl2= _kernel.Get<BusinessLogic_Two>();//InterfaceBvarbl1= _kernel.Get<BusinessLogic_One>();//InterfaceA}}
The Exception I get on the Failed test is:
Message: Test method UnitTests.Test_Calling_KernelGet_InterfaceBFirst threw exception:
System.InvalidCastException: Unable to cast object of type 'Castle.Proxies.InterfaceBProxy' to type 'InterfaceA'.
Would it be possible in the next version for this to be a Castle.Proxies.ImplementationProxy or something that both interfaces could inherit from?
The text was updated successfully, but these errors were encountered:
I have ran into an issue when binding two interfaces to the same concrete class when there is an Interceptor on the singleton binding.
Without the Interceptor these tests will all pass because when InterfaceA or InterfaceB is injected it is injected as the concrete class type. However with the interceptors it puts Castle.Proxies.InterfaceAProxy Or Castle.Proxies.InterfaceBProxy purely dependent upon which Kernel.Get was called first. Since InterfaceA inherits from InterfaceB, when InterfaceA goes first it can cast as the proxy for InterfaceB. But it will not work the other way around.
I have taken the complexity out of the instance I ran into and put it into a very basic scenario below. To run this snippet create a UnitTest Project in VisualStudio and add the Ninject.Extensions.Interception.DynamicProxy NuGet Package v3.3.3.
The Exception I get on the Failed test is:
Message: Test method UnitTests.Test_Calling_KernelGet_InterfaceBFirst threw exception:
System.InvalidCastException: Unable to cast object of type 'Castle.Proxies.InterfaceBProxy' to type 'InterfaceA'.
Would it be possible in the next version for this to be a Castle.Proxies.ImplementationProxy or something that both interfaces could inherit from?
The text was updated successfully, but these errors were encountered: