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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly the problem: in the case CanExecute is false, I set invoker.ReturnValueto the default value of the return type. And it works. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.