Simple injector, function not intercepted?
I am trying to switch to intercept my calls on my controller, but for some reason they are not intercepted.
Basically I'm trying to give an example to work: http://simpleinjector.readthedocs.org/en/latest/InterceptionExtensions.html
They have other info in the intercept section too: http://simpleinjector.readthedocs.org/en/latest/advanced.html
I have this feeling because I am not setting up the container correctly. Can anyone show me how I need to change my main one to see "Intercepted!!!"
after calls are made on the controller? Also, can someone tell me if the setup for the container was wrong and if so please explain my mistakes.
Code:
static void Main()
{
Console.WriteLine("Start");
RedisController2 redisController = new RedisController2();
Container _container = new Container();
_container.InterceptWith<MonitoringInterceptor>(type => type == typeof(IRedisController2));
_container.RegisterSingle<MonitoringInterceptor>();
redisController.PrintSomething();
redisController.PrintOther();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
internal class MonitoringInterceptor : IInterceptor
{
public MonitoringInterceptor()
{
}
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
//var decoratedType = invocation.InvocationTarget.GetType();
Console.Write("Intercepted!!!");
Console.ReadKey();
}
}
source to share
The problem stems from the fact that the Container does not create a controller and therefore cannot intercept calls made to it. Try the following:
Console.WriteLine("Start");
Container _container = new Container();
_container.Register<IRedisController2, RedisController2>(); // 1
_container.InterceptWith<MonitoringInterceptor>(type =>
type == typeof(IRedisController2));
_container.RegisterSingle<MonitoringInterceptor>();
IRedisController2 redisController =
_container.GetInstance<IRedisController2>(); // 2, 3
redisController.PrintSomething();
redisController.PrintOther();
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
Please note that I have:
- added code for controller registration
- added code to request controller instance from container
- changed the type created from
RedisController2
toIRedisController2
(sinceIRedisController2
is what was set up to intercept)
source to share
Assuming that it SimpleInjector
works the same as 90% of all IOC containers, it uses RealProxy
under the covers * (assuming the signature is likely, albeit completely against the spirit SimpleInjector
).
Note. The Interception extension code snippets use the .NETs System.Runtime.Remoting.Proxies.RealProxy class to generate interception proxies. RealProxy only allows proxy interfaces. [ source ]
RealProxy
works with interfaces for an obvious reason (if you think about it for a while). Even if it can work with a base class, this base class must declare its method as virtual
for your code.
Rebuild your code to use interfaces when invoked RedisController2
and remove all references to RedisController2
except when registering.
source to share