I’m using StructureMap to have an instance of an interface and I wrap it into a proxy with Castle DynamicProxy:
var proxy = generator.CreateInterfaceProxyWithTarget<T>(
ObjectFactory.GetInstance<T>()
, new SwitchInterceptor(isGranted, foundUser));
In the interceptor of type IInterceptor, I’ve got this code:
public override void Intercept(IInvocation invocation)
{
if (this.CanExecute)
{
invocation.Proceed();
}
}
When CanExecute is true, it always work but sometimes when CanExecute is false, I’ve got a weird NullReferenceException with a realy small stacktrace:
at Castle.Proxies.IGrantedReadProxy.ExecuteSomething()
I’m really lost and I don’t know where to look. Do you have any idea about what the problem is?
I think the problem is when the return type is a non-nullable value type (e.g.
int). In that case, the default return value ofnullthatinvocationhas is not applicable. And you don’t set it by callinginvocation.Proceed()either, so you have to set it another way.You have to explicitly set
invocation.ReturnValuein those cases. Another option is to throw a more informative exception.