Strange constraint violation problem with typed factories in Castle Windsor 2.5.1

148 views
Skip to first unread message

Lundberg, Per

unread,
Jan 20, 2011, 6:32:45 AM1/20/11
to castle-pro...@googlegroups.com

Hello,

 

We are using the Typed Factory facility in our application framework, to provide an easy way to be able to instantiate transient components (view models and entities, in our case).

 

This has worked well for some time now. However, recently, a very weird issue has popped up: in one of our project (not all of them), the factory that we are using is throwing an exception whose message looks like this:

 

          GenericArguments[0], 'T', on 'Castle.Proxies.Invocations.IEntityFactory_CreateEntity[T]' violates the constraint of type parameter 'T'.

 

The source of the exception is the DynamicProxyGenAssembly2. The class causing the exception is Castle.Proxies.IEntityFactoryProxy.CreateEntity[T]() – our factory interface is IEntityFactory, and the relevant method is defined like this:

 

        T CreateEntity<T>() where T : EntityBase;

 

The strange is that the T type that we are passing in to the method clearly derives from EntityBase (obviously in a correct manner, because otherwise the compiler would yell at us…)

 

Any suggestions/ideas? As mentioned above, we are still on Castle Windsor 2.5.1.

 

Best regards,

Per Lundberg

Krzysztof Koźmic

unread,
Jan 20, 2011, 7:34:46 AM1/20/11
to castle-pro...@googlegroups.com
Please submit a failing test case. Otherwise we'd be guessing.
--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

Lundberg, Per

unread,
Jan 21, 2011, 3:30:14 AM1/21/11
to castle-pro...@googlegroups.com

Hi Krzysztof,

 

The problem is that this is part of a bigger application, using a custom application framework built on top of Castle Windsor… so it’s not very isolated. Even more puzzling, the Typed Factory works correctly most of the time in other applications; it’s only in this specific application that we’re seeing this.

 

I did some further research: if I try to use the Typed Factory (IEntityFactory) at another place within the same application (even in the same assembly), it works flawlessly – which is very strange. So, the result is this:

 

1)      If I use the IEntityFactory from my PoFromWoModule, it works flawlessly – no exception. The PoFromWoModule is registered using “windsorContainer.Register(Component.For(type))” syntax, i.e. very “plain” registration.

2)      If I use the IEntityFactory from my RowsViewModel, it doesn’t work – I get the “type constraint violation” exception. The RowsViewModel is registered in a much more complex fashion, like this: “windsorContainer.Register(AllTypes.FromAssembly(assembly).Where((t) => t.IsSubclassOf(typeof(ViewModelBase))).Configure(c => c.LifeStyle.Singleton.Interceptors(typeof(IPropertySetInterceptor))));”. As you see, the main difference between these two components (PoFromWoModule and RowsViewModel) is that the latter uses interception.

3)      Even stranger: If I let both these components (PoFromWoModule and RowsViewModel) receive an IEntityFactory using property-injection, it works! It even works without the PoFromWoModule actually using the IEntityFactory; just receiving it from the property-based DI seems to make a difference somehow…

 

I wish I could send you an isolated test case, but it isn’t that straightforward unfortunately. If you like, however, I could possibly show you the code using Skype sharing or something like that. Would that be an option?

 

Best regards,

Per

 

From: castle-pro...@googlegroups.com [mailto:castle-pro...@googlegroups.com] On Behalf Of Krzysztof Kozmic
Sent: den 20 januari 2011 14:35
To: castle-pro...@googlegroups.com
Subject: Re: Strange constraint violation problem with typed factories in Castle Windsor 2.5.1

 

Please submit a failing test case. Otherwise we'd be guessing.

On 20/01/2011 9:32 PM, Lundberg, Per wrote:

Hello,

We are using the Typed Factory facility in our application framework, to provide an easy way to be able to instantiate transient components (view models and entities, in our case).

This has worked well for some time now. However, recently, a very weird issue has popped up: in one of our project (not all of them), the factory that we are using is throwing an exception whose message looks like this:

GenericArguments[0], 'T', on 'Castle.Proxies.Invocations.IEntityFactory_CreateEntity[T]' violates the constraint of type parameter 'T'.

The source of the exception is the DynamicProxyGenAssembly2. The class causing the exception is Castle.Proxies.IEntityFactoryProxy.CreateEntity[T]() our factory interface is IEntityFactory, and the relevant method is defined like this:

T CreateEntity<T>() where T : EntityBase;

The strange is that the T type that we are passing in to the method clearly derives from EntityBase (obviously in a correct manner, because otherwise the compiler would yell at us)

Konstantin

unread,
Jan 24, 2011, 7:26:00 AM1/24/11
to castle-pro...@googlegroups.com
I've ran into similar issue some time ago .
In few words it is most likely  .NET 3.5 issue and reproducable only under debugger.

Krzysztof Koźmic

unread,
Jan 24, 2011, 7:30:21 AM1/24/11
to castle-pro...@googlegroups.com
Yeah I remember that. Truly bizarre. And I do agree that it might be the
same issue.

Like I said before - I don't think there really is anything we can do as
(if it's indeed the same issue) the problem lies not in DynamicProxy but
in VS debugger.

Krzysztof

Lundberg, Per

unread,
Jan 26, 2011, 5:17:17 AM1/26/11
to castle-pro...@googlegroups.com
Hi,

Thank you guys for the response to this. You were right - we tried running
the application (which is .NET 3.5-based) outside of the debugger, and it
worked correctly, so it's obviously the debugger freaking out somehow.

Even stranger: if we resolve the typed factories at an "earlier place" in
the application lifetime, it also works. So this is a good-enough
workaround for us, until we can make the move to .NET 4.0. Maybe this can
also help someone else...

Best regards,
Per

-----Original Message-----
From: castle-pro...@googlegroups.com
[mailto:castle-pro...@googlegroups.com] On Behalf Of Krzysztof
Kozmic
Sent: den 24 januari 2011 14:30
To: castle-pro...@googlegroups.com
Subject: Re: Strange constraint violation problem with typed factories in
Castle Windsor 2.5.1

Yeah I remember that. Truly bizarre. And I do agree that it might be the
same issue.

Like I said before - I don't think there really is anything we can do as
(if it's indeed the same issue) the problem lies not in DynamicProxy but in
VS debugger.

Krzysztof

On 24/01/2011 10:26 PM, Konstantin wrote:
> I've ran into similar issue some time ago .
> Here is the
> discussion
> https://groups.google.com/d/topic/castle-project-users/40_6x7i1VBU/dis
> cussion In few words it is most likely .NET 3.5 issue and

Krzysztof Koźmic

unread,
Jan 26, 2011, 5:21:57 AM1/26/11
to castle-pro...@googlegroups.com
It actually makes sense - it depends on what type gets loaded with
regards to ordering of types in assembly header metadata. As I said -
it's an obscure debugger bug.
Reply all
Reply to author
Forward
0 new messages