System.InvalidCastException: Unable to cast object of type 'ObjectProxy

4,141 views
Skip to first unread message

bill richards

unread,
Sep 8, 2009, 8:42:50 AM9/8/09
to Rhino.Mocks
Am I really rubbish at searching the internet or what? I cannot find
any decent introductory documentation for Rhino Mocks anywhere!

I get the following exception when running the test below and I can
find no reference to this exception on the internet (but my rubbish-
ness(?!) at internet searching has already been highlighted).

System.InvalidCastException: Unable to cast object of type
'ObjectProxy86f050dbad4c447788658ebb5b463d54' to type
'Microsoft.Practices.Composite.Events.IEventAggregator'

THE TEST:

private const string MessageSent = "Services Module
Initialized";

[Test]
public void
WhenInstantiated_ShouldCallIStatusMessageServicePublishMessage()
{
var unity = MockRepository.GenerateStub<UnityContainer>();

var aggregator =
MockRepository.GenerateStub<IEventAggregator>();
var service =
MockRepository.GenerateStub<IStatusMessageService>();
var region = MockRepository.GenerateStub<IRegionManager>
();

unity.Stub(container => container.Resolve<IEventAggregator>
()).Return(aggregator); // throws exception
unity.Stub(container => container.Resolve<IRegionManager>
()).Return(region);
unity.Stub(container =>
container.Resolve<IStatusMessageService>()).Return(service);

new ServicesModule(unity).Initialize();

unity.AssertWasCalled(container =>
container.Resolve<IStatusMessageService>());
unity.AssertWasCalled(container =>
container.Resolve<IEventAggregator>());
service.AssertWasCalled(messageService =>
messageService.PublishStatusMessage(MessageSent));
}

Kenneth Xu

unread,
Sep 8, 2009, 8:56:26 AM9/8/09
to rhino...@googlegroups.com
I don't use Unity but is the Resolve a virtual method? In general, M$ mostly (if not all) uses non-virtual implementation.

Sent from my Verizon Wireless BlackBerry

-----Original Message-----
From: bill richards <bill.r...@greyskin.co.uk>

Date: Tue, 8 Sep 2009 05:42:50
To: Rhino.Mocks<rhino...@googlegroups.com>
Subject: [RhinoMocks] System.InvalidCastException: Unable to cast object of

bill richards

unread,
Sep 8, 2009, 9:04:24 AM9/8/09
to Rhino.Mocks
A little digging around indicates that your thoughts are correct, it
seems that I will instead have to construct my own MockUnityContainer
>         }- Hide quoted text -
>
> - Show quoted text -

bill richards

unread,
Sep 8, 2009, 9:05:38 AM9/8/09
to Rhino.Mocks
Of course I will also have to implement MockIEventAggregator, etc. too.

bill richards

unread,
Sep 8, 2009, 9:11:30 AM9/8/09
to Rhino.Mocks
Okay, what am I talking about!?

Resolve<T> is a virtual member.

However, the Prism examples tend to implement their own
MockUnityContainer, etc. I don't know it this is just because it's MS
code and they are not using a mocking framework, or if it is the
prefered method of mocking these objects.

Kenneth Xu

unread,
Sep 8, 2009, 11:08:05 AM9/8/09
to rhino...@googlegroups.com
The it should work with either IUnityContainer or UnitiContainer.

I would suggest to give Moq a try, that will tell if it is a Castle
DynamicProxy problem or RhinoMocks problem.

BTW, I'm also a user of RM and trying to help :)

bill richards

unread,
Sep 8, 2009, 11:10:23 AM9/8/09
to Rhino.Mocks
Any and all help is welcome, I will investigate further using Moq

Tuna Toksoz

unread,
Sep 8, 2009, 11:12:55 AM9/8/09
to rhino...@googlegroups.com
If it works with moq, don't switch to Moq :)

Tuna Toksöz
Eternal sunshine of the open source mind.

http://devlicio.us/blogs/tuna_toksoz
http://tunatoksoz.com
http://twitter.com/tehlike

Kenneth Xu

unread,
Sep 8, 2009, 11:32:03 AM9/8/09
to rhino...@googlegroups.com
Hi Bill,

> Okay, what am I talking about!?
>
> Resolve<T> is a virtual member.

How sad is it that there is no online API doc for Unity and the P&P
team want me to download an msi install file! I hope no other patterns
this approach at least.

Anyway I downloaded a copy of Unity.dll from somewhere else and opened
it up in Reflector.

No! it is not virtual as you can always expect from P&P, again hope
you don't pattern this :)

The method is defined in the UnityContainerBase:

public T Resolve<T>()
{
return (T) this.Resolve(typeof(T));
}

and it finally calls

public abstract object Resolve(Type t, string name);

which you can stub if you use partial mock.

HTH

bill richards

unread,
Sep 8, 2009, 11:45:48 AM9/8/09
to Rhino.Mocks
I (wrongly it would seem) deduced that it was virtual since it is
declared in the IUnityContainer interface .... now I'm in fear of not
knowing anything!! What is an interface if not a bunch of virtual
members?

bill richards

unread,
Sep 8, 2009, 11:47:29 AM9/8/09
to Rhino.Mocks
Oh my god! Are you telling me that I get this result by design? lol

On Sep 8, 4:12 pm, Tuna Toksoz <tehl...@gmail.com> wrote:
> If it works with moq, don't switch to Moq :)
>
> Tuna Toksöz
> Eternal sunshine of the open source mind.
>
> http://devlicio.us/blogs/tuna_toksozhttp://tunatoksoz.comhttp://twitter.com/tehlike
>
> On Tue, Sep 8, 2009 at 11:10 AM, bill richards <bill.richa...@greyskin.co.uk
>
>
>
> > wrote:
>
> > Any and all help is welcome, I will investigate further using Moq
>
> > On Sep 8, 4:08 pm, Kenneth Xu <kenne...@gmail.com> wrote:
> > > The it should work with either IUnityContainer or UnitiContainer.
>
> > > I would suggest to give Moq a try, that will tell if it is a Castle
> > > DynamicProxy problem or RhinoMocks problem.
>
> > > BTW, I'm also a user of RM and trying to help :)- Hide quoted text -

Kenneth Xu

unread,
Sep 8, 2009, 12:02:27 PM9/8/09
to rhino...@googlegroups.com
> I (wrongly it would seem) deduced that it was virtual since it is
> declared in the IUnityContainer interface .... now I'm in fear of not
> knowing anything!! What is an interface if not a bunch of virtual
> members?

Virtual member and interface members are very different. I don't have
a link handy but there are a lot of talks you can google, or go direct
to C# language specification.

I understand your point and believe everything should just be virtual
like Java. While I do love property, delegate, event, lamba and etc in
.Net, IMHO, this virtual/non-virtual thing is just unnecessary
complication introduced by M$ to say, hey .Net has one more feature
then Java.

That being said, you should mock the interface, not the implementation.

bill richards

unread,
Sep 8, 2009, 12:09:31 PM9/8/09
to Rhino.Mocks
> That being said, you should mock the interface, not the implementation.

This is my belief too, however I cannot do that using RM 3.6, and my
investigations have lead me to believe that one could instead mock the
implementation ... I seem to be going around in circels!

bill richards

unread,
Sep 8, 2009, 12:15:02 PM9/8/09
to Rhino.Mocks
using

var unity = MockRepository.GenerateMock<IUnityContainer>();

at System.Reflection.Emit.TypeBuilder._TermCreateClass(Int32 handle,
Module module)
at System.Reflection.Emit.TypeBuilder.CreateTypeNoLock()
at System.Reflection.Emit.TypeBuilder.CreateType()
at
Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType
()
at
Castle.DynamicProxy.Generators.Emitters.AbstractTypeEmitter.BuildType
()
at
Castle.DynamicProxy.Generators.InterfaceProxyWithTargetGenerator.GenerateCode
(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions
options)
at
Castle.DynamicProxy.DefaultProxyBuilder.CreateInterfaceProxyTypeWithoutTarget
(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options)
at
Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyTypeWithoutTarget
(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options)
at Castle.DynamicProxy.ProxyGenerator.CreateInterfaceProxyWithoutTarget
(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options, IInterceptor[] interceptors)
at Rhino.Mocks.MockRepository.MockInterface(CreateMockState
mockStateFactory, Type type, Type[] extras)
at Rhino.Mocks.MockRepository.CreateMockObject(Type type,
CreateMockState factory, Type[] extras, Object[]
argumentsForConstructor)
at Rhino.Mocks.MockRepository.DynamicMock[T](Object[]
argumentsForConstructor)
at Rhino.Mocks.MockRepository.<>c__DisplayClass7`1.<GenerateMock>b__6
(MockRepository r)
at Rhino.Mocks.MockRepository.CreateMockInReplay[T](Func`2 createMock)
at Rhino.Mocks.MockRepository.GenerateMock[T](Object[]
argumentsForConstructor)
at
gsdc.demo.calculator.modules.services.ServicesModuleTests.WhenInstantiated_ShouldCallIStatusMessageServicePublishMessage
() in D:\prototypes\CompositeCalculator\Module.Services.Tests
\ServicesModuleTests.cs:line 18

using

var unity = MockRepository.GenerateMock<UnityContainer>();

at Microsoft.Practices.Unity.UnityContainerBase.Resolve[T]()
at
gsdc.demo.calculator.modules.services.ServicesModuleTests.<WhenInstantiated_ShouldCallIStatusMessageServicePublishMessage>b__0
(UnityContainer container) in D:\prototypes\CompositeCalculator
\Module.Services.Tests\ServicesModuleTests.cs:line 24
at Rhino.Mocks.RhinoMocksExtensions.Expect[T,R](T mock, Function`2
action)
at Rhino.Mocks.RhinoMocksExtensions.Stub[T,R](T mock, Function`2
action)
at
gsdc.demo.calculator.modules.services.ServicesModuleTests.WhenInstantiated_ShouldCallIStatusMessageServicePublishMessage
() in D:\prototypes\CompositeCalculator\Module.Services.Tests
\ServicesModuleTests.cs:line 24

Tuna Toksoz

unread,
Sep 8, 2009, 12:52:14 PM9/8/09
to rhino...@googlegroups.com
No, It is a feature. It tries to keep you away from unity :)

Kenneth Xu

unread,
Sep 8, 2009, 1:27:14 PM9/8/09
to rhino...@googlegroups.com
Let's wait for Tim to get back to us of which DP version was 3.6
compiled with. I didn't have time at this moment to try this out
myself. But in the mean time, you can try with a copy of RM 3.5 that I
compiled with a version of DynamicProxy that has a lot of bug fixed:
http://code.google.com/p/kennethxublogsource/downloads/list

I was getting bad image a lot at those days and hence my own compilation of RM.

Tim Barcz

unread,
Sep 8, 2009, 2:40:11 PM9/8/09
to rhino...@googlegroups.com
Sorry for the delay...

Castle.DynamicProxy2.dll = 2.1.0.5625

Tim
--
Tim Barcz
Microsoft ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

bill richards

unread,
Sep 8, 2009, 2:56:44 PM9/8/09
to Rhino.Mocks
Using Rhino-Mocks-net-3.5-v3.5.zip downloaded from your link Kenneth:

gsdc.demo.calculator.modules.services.ServicesModuleTests.WhenInstantiated_ShouldCallIStatusMessageServicePublishMessage:
System.InvalidCastException : Unable to cast object of type
'Castle.Proxies.ObjectProxy77dff2d4f46746c8b2cc2c8ee4793186' to type
'Microsoft.Practices.Composite.Events.IEventAggregator'.

bill richards

unread,
Sep 8, 2009, 3:18:54 PM9/8/09
to Rhino.Mocks
Sorry Kenneth, I take back my last post ... that eror was because I
had not changed my ock to the interface, however when I made this
change, it worked as expected

On Sep 8, 7:56 pm, bill richards <bill.richa...@greyskin.co.uk> wrote:
> Using Rhino-Mocks-net-3.5-v3.5.zip downloaded from your link Kenneth:
>
> gsdc.demo.calculator.modules.services.ServicesModuleTests.WhenInstantiated_­ShouldCallIStatusMessageServicePublishMessage:

Kenneth Xu

unread,
Sep 8, 2009, 3:34:57 PM9/8/09
to rhino...@googlegroups.com
Great!

Now it is your choice of keeping my version of RM (with many enhanced
feature that you (most likely) won't get it in future RM releases, or
get the RM source and compile one yourself with latest DynamicProxy2.

bill richards

unread,
Sep 8, 2009, 3:41:51 PM9/8/09
to Rhino.Mocks
Thanks for your help here guys, it is much appreciated.

I'm going to carry on with your version, but try my hardest not to
utilize your extra feature set, so that when the next official build
is released (hopefully including DynamicProxy2) we can get back to a
current and supported library.
> > change, it worked as expected- Hide quoted text -

Tim Barcz

unread,
Sep 8, 2009, 4:18:52 PM9/8/09
to rhino...@googlegroups.com
This makes me wonder if we should pull down the latest DynamicProxy and make a new 3.6.1 drop.

Thoughts?

Ayende Rahien

unread,
Sep 8, 2009, 4:22:36 PM9/8/09
to rhino...@googlegroups.com
No problem from my side.

Tim Barcz

unread,
Sep 8, 2009, 4:24:11 PM9/8/09
to rhino...@googlegroups.com
On Git? or SourceForge? (SourceForge is easier for me right now, but will do Git if we've migrated)

Tim

Ayende Rahien

unread,
Sep 8, 2009, 4:28:12 PM9/8/09
to rhino...@googlegroups.com
I would prefer doing it on git.
There are some changes that are only there.

Tim Barcz

unread,
Sep 8, 2009, 4:51:23 PM9/8/09
to rhino...@googlegroups.com
I'll be the dunce in the room....what is the Git url for the project?

tim

Ayende Rahien

unread,
Sep 8, 2009, 4:53:58 PM9/8/09
to rhino...@googlegroups.com

Alex McMahon

unread,
Sep 9, 2009, 11:07:04 AM9/9/09
to rhino...@googlegroups.com
Tim's not the only dunce in the room, I couldn't find it earlier
myself. If the source has been migrated and is going to stay there can
we add/update links to the source e.g. at
http://ayende.com/projects/rhino-mocks/downloads.aspx
Also Ayende, could you maybe post a blog post to say that the source
for Rhino Mocks is now on Git...

2009/9/8 Ayende Rahien <aye...@ayende.com>:

Tim Barcz

unread,
Sep 9, 2009, 10:24:31 PM9/9/09
to rhino...@googlegroups.com
Bill/All,

This was fixed today in "trunk".  We'll be releasing a new build (called RhinoMocks 3.6.1 or 3.7 not sure yet), which fixes the BadImageFormatException.

In fact I wrote a test using Microsoft's Unity DLL which failed, then we got that particular test to pass.

Tim

Krzysztof Koźmic (2)

unread,
Sep 18, 2009, 7:57:35 AM9/18/09
to Rhino.Mocks
I plan (depending on free time which is a little bit hard to predict)
to get a feature complete beta of DynamicProxy v2.2 by the end of
november, and if there are no major issues release v2.2 before the end
of the year...
Just to let you know, so that you can better decide whether to wait
for the release, or to go ahead now, and possibly do a 3.7 when DP 2.2
gets released.

As Kenneth mentioned there are quite a lot of fixes, plus some new
scenarios are enabled. I tried not to introduce breaking changes so
hopefully it will just work.

Krzysztof

On 8 Wrz, 22:28, Ayende Rahien <aye...@ayende.com> wrote:
> I would prefer doing it on git.There are some changes that are only there.
>
> On Tue, Sep 8, 2009 at 11:24 PM, Tim Barcz <timba...@gmail.com> wrote:
> > On Git? or SourceForge? (SourceForge is easier for me right now, but will
> > do Git if we've migrated)
>
> > Tim
>
> > On Tue, Sep 8, 2009 at 3:22 PM, Ayende Rahien <aye...@ayende.com> wrote:
>
> >> No problem from my side.
>
Reply all
Reply to author
Forward
0 new messages