views:

93

answers:

1

I've just downloaded the dynamic object framework Clay and am running into issues regarding castle project versions. Clay uses functionality from v2.0 of "castle" whilst I have a project which has been started referencing v2.5. Needless to say just to make matters more interesting I'm a complete beginner in all things "Castle" and IoC.

The real problem is that upgrading the references within clay solution results in a depreciated method warning. Regardless of whether you supress the method or not, the provided unit tests fail with a "Cannot perform runtime binding on a null reference" exception in the following code in "Intercept" of "InterfaceProxyBehavior":

       var invoker = BindInvoker(invocation);
       invoker(invocation);

The code that produces the run-time warning is in "CreateInstance" of "DefaultClayActivator":

       //var proxyType = _builder.CreateClassProxy(baseType, options);
       var proxyType = _builder.CreateClassProxyType(baseType, null, options);

As previously stated I'm still a complete beginner with Castle Windsor and just starting out with IoC so haven't even come across the Proxy stuff yet. Frustratingly I have no idea what the error message is even telling me, or asking for.

Have anyone already ported Clay across to version 2.5 of the castle project, so know the steps needed. Or can any one with experience of this part of castle throw anymore light on the error and what I may need to do to resolve it.

Updated

I'm still not really any the wiser as to the functionality that is failing, but have had chance to revisit the code running it both with v2.0 (works) and v2.5 (breaks) in castle.core. Attached are two images of the debug information when it works and then when it breaks. The test that it fails on is below, I've indicated the call with a comment.

namespace ClaySharp.Tests {
    [TestFixture]
    public class BinderFallbackTests {
       ...
    [Test]
    public void TestInvokePaths() {
        var dynamically = ClayActivator.CreateInstance<Alpha>(new IClayBehavior[] { 
            new InterfaceProxyBehavior(), 
            new AlphaBehavior() 
        });
        Alpha statically = dynamically;
        IAlpha interfacially = dynamically;

        Assert.That(dynamically.Hello(), Is.EqualTo("World-"));
        Assert.That(statically.Hello(), Is.EqualTo("World-"));
        Assert.That(interfacially.Hello(), Is.EqualTo("World-"));  // <- Fails on this call

        Assert.That(dynamically.Foo(), Is.EqualTo("Bar-"));
        Assert.That(interfacially.Foo(), Is.EqualTo("Bar-"));

        Assert.Throws<RuntimeBinderException>(() => dynamically.MissingNotHandled());
    }
    ...
  }
}

This is the debug information when using v2.5 of castle.core and the exception is thrown: alt text

This is the debug information using v2.0 of castle.core (which works) for the same call / line that causes the problem with v2.5 alt text

+1  A: 

I have never used this Clay thing, so all stuff below is based on assumptions.

The error message from BindInvoker is not a Castle error, but I'm guessing it's happening because the invoker is trying to bind to invocation target of the proxy which in DynamicProxy 2.1 used to have a value in some cases, which was wrong, and later versions 2.2 and 2.5 fixed that but it was a breaking change that you're now experiencing.

The other error message is telling you

Use CreateClassProxyType method instead.

Which is the other method you commented out. What's not obvious here?

Krzysztof Koźmic
Thanks @Krzysztof, in the 2nd code section the line that is commented out is the original code using v2.0 - this creates a design time warning/error recommending using the 2nd format. Regardless of which format I use I get the null run time binding exception on the "invoker(invocation);" code. Is the solution to remove the call to invoker in certain circumstances, if so what check can I make? Sorry, really just starting out with DynamicProxy, etc.
Paul Hadfield
No worries Paul. The other issue is more complicated to fix. There was a discussion and explanation of this topic on MoQ discussion group. I can try to dig it up later. Cheers.
Krzysztof Koźmic
Updated question with extra debug info
Paul Hadfield
Thanks Krsysztof, I'm going to mark this as answered as I feel / it looks like you have given me the solution. It's not your fault that at the moment I don't know what to do with it :) I'll continue having a play and am trying to speak to the guys behind clay. I'll update the question when I find or figure out what's needed.
Paul Hadfield