Hello,
I have a couple of components that has generic methods like Get<T>();
Both components register ok. However resolving the 2nd components causing an exception:
violates the constraint of type parameter:
Method xxx.GetProxy: type argument xxx.ILoginService' violates the constraint of type parameter 'T'.

I would appreciate any help.
Best,
Ido
I have the following components
public interface IServiceAgent
{
T GetProxy<T>() where T : class;
T GetProxy<T>(object callbackInstance) where T : class;
}
And
public interface IPresentationHost
{
void Register<T>() where T : IPresentation;
void Register<T>(Type service) where T : IPresentation;
}
Both has simple implementation that do not do anything (stub objects) IServiceAgent actually return null.
The calling code is as follow:
IServiceAgent agent = FourDPlatform.Loader.Resolve<IServiceAgent>();
ILoginService proxy = agent.GetProxy<ILoginService>();
I get the error on the ILoginService.
The weird is that if I do not register the IPresentationHost to the container, then resolving of the ILoginService works fine.
There is no reference or relation between the ILoginService and the IPresentationHost. Both are completely decoupled.
I wrote a small sample that present the problem. It happens when my object implements two interfaces (one is IStartable)
I Added a comment near the problem
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using Castle.Core.Interceptor;
using Castle.MicroKernel;
using Castle.MicroKernel.Facilities;
using Castle.MicroKernel.ModelBuilder;
using Castle.Windsor.Proxy;
using Castle.Facilities.Startable;
using Castle.Core;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
IKernel k = new DefaultKernel(new DefaultProxyFactory());
k.AddFacility("startable",new StartableFacility());
k.AddFacility("tracing", new GenericFacility<TraceInterceptor>());
k.AddComponent<PresentationHost>(typeof(IPresentationHost));
k.AddComponent<ServiceAgent>(typeof(IServiceAgent));
// For some reason there is a connection between IServiceAgent and IPresentation since it requires now an IPresentation. Though both are completely decoupled.
k.Resolve<IServiceAgent>().GetProxy<MyPresentation>(); // This line works
k.Resolve<IServiceAgent>().GetProxy<string>();// This line fails
}
}
public class PresentationHost : IPresentationHost, IStartable
{
#region IPresentationHost Members
public void Register<T>() where T : IPresentation
{
}
public void Register<T>(Type service) where T : IPresentation
{
}
#endregion
#region IStartable Members
public void Start()
{
}
public void Stop()
{
}
#endregion
}
public class ServiceAgent : IServiceAgent
{
#region IServiceAgent Members
public T GetProxy<T>() where T : class
{
return null;
}
public T GetProxy<T>(object callbackInstance) where T : class
{
return null;
}
#endregion
}
public interface IPresentationHost
{
void Register<T>() where T : IPresentation;
void Register<T>(Type service) where T : IPresentation;
// void Register(Type classType); // THIS present another bug , castle scream that Register is defined already. Not the main concern though.
// void Register(Type classType, Type serviceType);
}
public interface IServiceAgent
{
T GetProxy<T>() where T : class;
T GetProxy<T>(object callbackInstance) where T : class;
}
public interface IPresentation
{
}
public class MyPresentation : IPresentation
{
}
#region helpers
[Serializable]
public class DefaultInterceptor : IInterceptor
{
private bool enabled;
public DefaultInterceptor()
{
enabled = true;
}
public bool Enabled
{
get { return enabled; }
set { enabled = value; }
}
public virtual void Intercept(IInvocation invocation)
{
if (Enabled)
PreProceed(invocation);
invocation.Proceed();
if (Enabled)
PostProceed(invocation);
}
protected virtual void PreProceed(IInvocation invocation)
{
}
protected virtual void PostProceed(IInvocation invocation)
{
}
}
internal class TraceInterceptor : DefaultInterceptor
{
static TraceInterceptor()
{
System.Diagnostics.Trace.Listeners.Add(new ConsoleTraceListener());
Trace.AutoFlush = true;
}
protected override void PreProceed(IInvocation invocation)
{
//Trace.Indent();
// type = invocation.InvocationTarget.GetType();
// if (type.BaseType != typeof(System.Object))
// type = type.BaseType;
string threadName = Thread.CurrentThread.Name ?? "???";
Trace.WriteLine(string.Format("[{0}] {1}.{2}", threadName, invocation.TargetType.Name, invocation.MethodInvocationTarget.Name));
}
protected override void PostProceed(IInvocation invocation)
{
// Trace.WriteLine(string.Format("After '{1}' ", type.FullName, invocation.MethodInvocationTarget.Name), category);
// Trace.Unindent();
}
}
public class GenericFacility<T> : AbstractFacility where T : class, IInterceptor
{
protected override void Init()
{
Kernel.AddComponent<T>();
Kernel.ComponentModelBuilder.AddContributor(new DefaultComponentInspector<T>());
}
}
public class DefaultComponentInspector<T> : IContributeComponentModelConstruction where T : IInterceptor
{
public virtual void ProcessModel(IKernel kernel, ComponentModel model)
{
model.Dependencies.Add(
new DependencyModel(DependencyType.Service, null, typeof(T), false));
model.Interceptors.Add(
new InterceptorReference(typeof(T)));
}
}
#endregion
}
From: castle-pro...@googlegroups.com [mailto:castle-pro...@googlegroups.com] On Behalf Of James Curran
Sent: Wednesday, November 14, 2007 4:03 PM
To: castle-pro...@googlegroups.com
// void Register(Type classType); // THIS present another bug , castle scream that Register is defined already.
Well. Not always I know the type explicitly in my code. Sometimes I have it dynamically. This is why I require the two methods. Though I can still use the implicit one. I agree on that.
Any how, this was not the main issue where I get the violation exception. Any comments on this one??
From:
castle-pro...@googlegroups.com
[mailto:castle-pro...@googlegroups.com] On Behalf Of James Curran
Sent: Thursday, November 15, 2007 3:25 AM
To: castle-pro...@googlegroups.com
Subject: Re: violates the constraint of type parameter 'T'
Well, after carefully stepping through the code, and my only answer is --- Damnned if I know.
What really blew my mind was when I noticed the generic restrictions passed to a different object (where T : IPresentation).
I removed the restriction and added casting and now everything works fine. However, this is still a bug I would really like to fix. If someone can point me out to where exactly I should look, I’ll fix it.
Best,
Ido
Further to this - I've just run this and it works perfectly.
--output--
[???] ServiceAgent.GetProxy
[???] ServiceAgent.GetProxy
Press any key to continue . . .
--/output--
It fails if it's run under the debugger though. Weird.
It only fails if you include your Interceptor - so I'd start looking there.
j.
I did. This really looks like in castle.
From:
castle-pro...@googlegroups.com
[mailto:castle-pro...@googlegroups.com] On Behalf Of Ayende
Rahien
Sent: Thursday, November 15, 2007 6:27 AM
To: castle-pro...@googlegroups.com
Subject: Re: violates the constraint of type parameter 'T'
Just to point out, there are many bugs related to generics in the BCL and runtime itself.
-----Original Message-----
From: castle-pro...@googlegroups.com
[mailto:castle-pro...@googlegroups.com] On Behalf Of josh robb
Sent: Thursday, November 15, 2007 6:53 AM
To: castle-pro...@googlegroups.com
Subject: Re: violates the constraint of type parameter 'T'
That's a hard argument to make given that this problem only occurs
when running under a debugger.
j.
I remember you mentioning..... :(
I've got a reproducible test case...
The problem is that if you have a constraint on a generic method then
you get the dreaded:
"An attempt was made to load a program with an incorrect format.
(Exception from HRESULT: 0x8007000B)"
j.
When I use the debugger I get the failure on the first resolve too -
i.e. the one with the comment that says "// This line works". If I
remove the line to register the startable facility everything works
again so I'm not convinced the interceptor is the cause of the problem
as this is still there. The only real difference is that the
PresentationHost is not instantiated.
Dave.
-----Original Message-----
From: castle-pro...@googlegroups.com
[mailto:castle-pro...@googlegroups.com] On Behalf Of josh robb
Sent: 15 November 2007 11:53
To: castle-pro...@googlegroups.com
Subject: Re: violates the constraint of type parameter 'T'
-----Original Message-----
From: castle-pro...@googlegroups.com
[mailto:castle-pro...@googlegroups.com] On Behalf Of josh robb
Sent: Thursday, November 15, 2007 9:07 AM
To: castle-pro...@googlegroups.com
Subject: Re: violates the constraint of type parameter 'T'
-----Original Message-----
From: castle-pro...@googlegroups.com
[mailto:castle-pro...@googlegroups.com] On Behalf Of josh robb
Sent: Thursday, November 15, 2007 8:35 AM
To: castle-pro...@googlegroups.com
Subject: Re: violates the constraint of type parameter 'T'
I will take a deeper look at it.
From:
castle-pro...@googlegroups.com
[mailto:castle-pro...@googlegroups.com] On Behalf Of Ayende
Rahien
Sent: Thursday, November 15, 2007 4:35 PM
To: castle-pro...@googlegroups.com
Subject: Re: violates the constraint of type parameter 'T'
There are several runtime bugs
that are exposed by Castle. They are mostly involving generics, reflection emit
etc.
This could be one of those.
On 11/15/07, Ido Samuelson <ido.sa...@gmail.com> wrote:
I got lost in the discussion :) so is this an official bug now? Or is a
problem on my end?
-----Original Message-----
From: castle-pro...@googlegroups.com
[mailto:castle-pro...@googlegroups.com]
On Behalf Of josh robb
Sent: Thursday, November 15, 2007 9:07 AM
To: castle-pro...@googlegroups.com
Subject: Re: violates the constraint of type parameter 'T'
On Nov 15, 2007 1:37 PM, Ayende Rahien <aye...@ayende.com
> wrote:
> Hm, there are several runtime issues that are exposed only there, IIRC
I remember you mentioning..... :(
I've got a reproducible test case...
The problem is that if you have a constraint on a generic method then
you get the dreaded:
"An attempt was made to load a program with an incorrect format.
(Exception from HRESULT: 0x8007000B)"
j.
<br
Yep - except it's the generic type parameter of a generic method.
The only thing that's strange is that it doesn't fault when using a
CreateClassProxy, only CreateInterfaceProxy*
I haven't had time to dig into that yet.
j.
If you mean an offical CLR bug then maybe. If you mean an official
Castle bug - then not yet. (and honestly - ATM it looks like it's not
going to be treated as such).
j.
Though I don't really have the time, either, I might be able to take
some to look into this on Monday or Tuesday. (After all, I'm the
generics guy or something.) Do you have a DynamicProxy-only test case
exposing the problem?
Fabian
Yay. it only occurs when running under the debugger.
RhinoMocksTestCase.ProxyingGenericClassWithGenericClassConstraint
j.
I am using Orcas Beta 2. If this bug is fixed then either beta 2 do not hold this fix or this one is different one.
From: castle-pro...@googlegroups.com
[mailto:castle-pro...@googlegroups.com] On Behalf Of Ayende
Rahien
Sent: Friday, November 16, 2007 1:44 PM
To: castle-pro...@googlegroups.com
Subject: Re: violates the constraint of type parameter 'T'
Okay, that is a runtime bug I
think.
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=262509
I will prepare one for you by Monday.
-----Original Message-----
From: castle-pro...@googlegroups.com
[mailto:castle-pro...@googlegroups.com] On Behalf Of Fabian Schmied
Sent: Friday, November 16, 2007 11:33 AM
To: castle-pro...@googlegroups.com
Subject: Re: violates the constraint of type parameter 'T'
I finally found some time to look into this and have managed to
encapsulate the problem into a DP2-only test case. I've checked the
code generated by DynamicProxy 2, it is perfectly verifiable. Also,
the behavior is incredibly fragile to reproduce:
- The code fails only from within the debugger,
- It fails only if a second proxy was generated before the method is called,
- It fails only if dynamically generated code is executed; if the
proxy is saved and reloaded, it doesn't.
Therefore, I'm really sure that this is a CLR bug, which,
unfortunately, still exists in VS 2008. I've reported this on
Microsoft Connect, but I guess if it is fixed, it won't be in the near
future.
Sorry I can't do much more, it's a runtime issue at the moment.
Fabian
Thanks Fabian! I'm just glad that it wasn't something obviously dumb
that I missed. :)
j.
Here's a follow up on my behalf which just confirms that this problem
still exists.
We got the same problem in our code base (we're using VS.Net 2008
SP1 / .net 3.5 SP1 and MSTest).
Since over a year or so we started to experience the problem that we
were unable to run our tests using the Debug configuration in VS.Net.
However, if we switch over to Release mode, everything works out just
fine.
Just before I found this thread I tried to turn on debug options for
our Release config of the test project (Project settings -> Build ->
Advanced -> Debug Info).
This is my conclusion:
- Having Debug Info set to none, everything works out just fine
- Having Debug Info set to full or pdb only, the following message
appears:
Initialization method
ApplicationTests.x.Presenters.xPresenterTests.Init threw exception.
System.TypeLoadException: System.TypeLoadException: GenericArguments
[0], 'TService', on
'IxyzProxyb09635780bbe42aa9a8d37e644190956+InvocationCreateHandler_13
[TContentType]' violates the constraint of type parameter
'TContentType'..
The line of code in the TestInitialize block:
SetupResult.For( xMock.CreateHandler<xyz>() ).Return( z );
First of all I supposed it was some conditional code that was the
reason (something triggered by #if !DEBUG ... ), but note that I only
changed Debug Info - I did not turn on the DEBUG compile time constant
to reproduce the error for our Release configuration.
I certainly feels like something related to the mix of generated code
and compiled code with debug info.
- Johan
On 22 Nov 2007, 14:32, "Fabian Schmied" <fabian.schm...@gmail.com>
wrote: