I'm trying to add an interceptor for all of my controllers. I'm using Simple Injector and asp.net MVC.
While trying this example: https://simpleinjector.readthedocs.io/en/latest/InterceptionExtensions.html everything worked when I wanted to add the interceptor to an interface. The problem starts with Controllers since they are concrete type and not interfaces...
private static void ThrowIfServiceTypeNotInterface(ExpressionBuiltEventArgs e) {
// NOTE: We can only handle interfaces, because
// System.Runtime.Remoting.Proxies.RealProxy only supports interfaces.
if (!e.RegisteredServiceType.IsInterface) {
throw new NotSupportedException("Can't intercept type " +
e.RegisteredServiceType.Name + " because it is not an interface.");
}
}
In the implementation above there is an explicit restriction to interfaces only, I would like to understand why?
Does anyone have experience in adding interceptors to SimpleInjector for Controllers / concrete types?
In the implementation above there is an explicit restriction to interfaces only, I would like to understand why?
Well, the comment already answers that question:
// NOTE: We can only handle interfaces, because
// System.Runtime.Remoting.Proxies.RealProxy only supports interfaces.
To be able to apply interception, you will have to make sure that the controllers are resolved by the IController
interface. You will find how to achieve this in this q/a: How to decorate an ASP.NET MVC controller with Simple Injector.