2

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?

JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57

1 Answers1

2

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 of null that invocation has is not applicable. And you don't set it by calling invocation.Proceed() either, so you have to set it another way.

You have to explicitly set invocation.ReturnValue in those cases. Another option is to throw a more informative exception.

svick
  • 236,525
  • 50
  • 385
  • 514
  • That's exactly the problem: in the case `CanExecute` is `false`, I set `invoker.ReturnValue`to the default value of the return type. And it works. Thank you very much! – JiBéDoublevé Nov 23 '11 at 12:39