DynamicProxy crashes under debug

109 views
Skip to first unread message

Konstantin

unread,
Oct 12, 2010, 8:33:25 AM10/12/10
to Castle Project Users
Following code causes exception if executed under VS2008 debugger.

Exception:
System.TypeLoadException: GenericArguments[0], 'ST2', on
'Castle.Proxies.Invocations.IService_Method2[ST1]' violates the
onstraint of type parameter 'ST1'.
at Castle.Proxies.IServiceProxy.Method2[ST2](ST2 t)
at DPGenericsUnderDebugIssueDemo.Program.Main(String[] args) in C:
\WORK\TestsAndTries\DPGenericsUnderDebugIssueDemo
\DPGenericsUnderDebugIssueDemo\Program.cs: line 61


This:
"GenericArguments[0], 'ST2', on
'Castle.Proxies.Invocations.IService_Method2[ST1]'"
looks very strange....


If executed without debugger it work ok.


public class C : ICloneable
{
public object Clone()
{
throw new NotImplementedException();
}
}


public interface IService
{
void Method1<ST1>(ST1 t) where ST1 : class, new();
void Method2<ST2>(ST2 t) where ST2 : ICloneable;
}

public class Component : IService
{
public void Method1<CT1>(CT1 t) where CT1 : class, new()
{
Console.WriteLine(string.Format("Method1 CT1:{0}", typeof
(CT1)));
}

public void Method2<CT2>(CT2 t) where CT2 : ICloneable
{
Console.WriteLine(string.Format("Method2 CT2:{0}", typeof
(CT2)));
}
}

internal class Program
{
[SecurityPermission(SecurityAction.Deny, Unrestricted = true,
Flags = SecurityPermissionFlag.UnmanagedCode)]
private static void tuneDynamicProxy()
{
Console.WriteLine(StrongNameUtil.CanStrongNameAssembly);
}


static Program()
{
//Restrict signed assembly generation
tuneDynamicProxy();
}

private static void Main(string[] args)
{
ProxyGenerator g = new ProxyGenerator();
var proxy = g.CreateInterfaceProxyWithTarget<IService>(new
Component(), new IInterceptor[0]);


try
{
proxy.Method1(new C());
proxy.Method2(new C());
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
Console.ReadLine();
}
}
}

Krzysztof Koźmic

unread,
Oct 12, 2010, 8:55:44 AM10/12/10
to castle-pro...@googlegroups.com
I ran it on .NET 4 under vs2010 and the code does not work at all.

Interestingly - for completely different reasons:

Unhandled Exception: System.TypeInitializationException: The type
initializer for 'ConsoleApplication9.Program' threw an exception. --->
System.TypeInitializationException: The type initializer for
'Castle.DynamicProxy.Generators.Emitters.StrongNameUtil' threw an
exception. ---> System.NotSupportedException: The Deny stack modifier
has been obsoleted by the .NET Framework. Please see
http://go.microsoft.com/fwlink/?LinkId=155571 for more information.
at System.Security.CodeAccessSecurityEngine.Check(Object demand,
StackCrawlMark& stackMark, Boolean isPermSet)
at
System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap,
StackCrawlMark& stackMark)
at System.Security.CodeAccessPermission.Demand()
at Castle.DynamicProxy.Generators.Emitters.StrongNameUtil..cctor()
in
c:\TeamCity\buildAgent\work\f0c43d96621436a8\src\Castle.Core\DynamicProxy\Generators\Emitters\StrongNameUtil.cs:line
40
--- End of inner exception stack trace ---
at
Castle.DynamicProxy.Generators.Emitters.StrongNameUtil.get_CanStrongNameAssembly()
at ConsoleApplication9.Program.tuneDynamicProxy() in
c:\users\xtoff\documents\visual studio
2010\Projects\ConsoleApplication9\ConsoleApplication9\Program.cs:line 58
at ConsoleApplication9.Program..cctor() in
c:\users\xtoff\documents\visual studio
2010\Projects\ConsoleApplication9\ConsoleApplication9\Program.cs:line 52
--- End of inner exception stack trace ---
at ConsoleApplication9.Program.Main(String[] args)

Notice it fails even before any attempt to generate the proxy.
Anyway - I remember there was some bug in VS2008 debugger, so you may
have hit just that.

Which version of DP are you running?

Konstantin

unread,
Oct 12, 2010, 9:30:25 AM10/12/10
to Castle Project Users
Remove tuneDynamicProxy() call and restrict creation of signed
assemby in some other way. Issue can be repeoduced where DP creates
only unsigned assembly.
The check for whether to create unsigned assembly or not is based on
SecurityPermissionFlag.UnmanagedCode check.




On 12 окт, 16:55, Krzysztof Koźmic <krzysztof.koz...@gmail.com> wrote:
>   I ran it on .NET 4 under vs2010 and the code does not work at all.
>
> Interestingly - for completely different reasons:
>
> Unhandled Exception: System.TypeInitializationException: The type
> initializer for 'ConsoleApplication9.Program' threw an  exception. --->
> System.TypeInitializationException: The type initializer for
> 'Castle.DynamicProxy.Generators.Emitters.StrongNameUtil' threw an
> exception. ---> System.NotSupportedException: The Deny stack modifier
> has been obsoleted by the .NET Framework.  Please seehttp://go.microsoft.com/fwlink/?LinkId=155571for more information.
>     at System.Security.CodeAccessSecurityEngine.Check(Object demand,
> StackCrawlMark& stackMark, Boolean isPermSet)
>     at
> System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap,
> StackCrawlMark& stackMark)
>     at System.Security.CodeAccessPermission.Demand()
>     at Castle.DynamicProxy.Generators.Emitters.StrongNameUtil..cctor()
> in
> c:\TeamCity\buildAgent\work\f0c43d96621436a8\src\Castle.Core\DynamicProxy\G enerators\Emitters\StrongNameUtil.cs:line

Konstantin

unread,
Oct 12, 2010, 9:33:31 AM10/12/10
to Castle Project Users
I've tried it with 2010.
To run this code with clr 4.0 you need to add following to app.comfig
<configuration>
<runtime>
<NetFx40_LegacySecurityPolicy enabled="true"/>
</runtime>
</configuration>

But with 4.0 it works.

Change target framework to 3.5 to reproduce the issue



On 12 окт, 17:30, Konstantin <konstantin.no...@gmail.com> wrote:
> Remove   tuneDynamicProxy() call and restrict creation of signed
> assemby in some other way. Issue can be repeoduced where DP creates
> only unsigned assembly.
> The check for whether to create unsigned assembly or not is based on
> SecurityPermissionFlag.UnmanagedCode check.
>
> On 12 окт, 16:55, Krzysztof Koźmic <krzysztof.koz...@gmail.com> wrote:
>
>
>
> >   I ran it on .NET 4 under vs2010 and the code does not work at all.
>
> > Interestingly - for completely different reasons:
>
> > Unhandled Exception: System.TypeInitializationException: The type
> > initializer for 'ConsoleApplication9.Program' threw an  exception. --->
> > System.TypeInitializationException: The type initializer for
> > 'Castle.DynamicProxy.Generators.Emitters.StrongNameUtil' threw an
> > exception. ---> System.NotSupportedException: The Deny stack modifier
> > has been obsoleted by the .NET Framework.  Please seehttp://go.microsoft.com/fwlink/?LinkId=155571formore information.

Krzysztof Koźmic

unread,
Oct 12, 2010, 9:46:42 AM10/12/10
to castle-pro...@googlegroups.com
yes, when I remove the call all works fine.

Konstantin

unread,
Oct 12, 2010, 10:08:53 AM10/12/10
to Castle Project Users
I does not work fine with the call and target framework 3.5.
I've saved the generated assembly and referenced it instead of using
DP, and in this config it works.
So the problem occures when DP emits unsigned assembly under VS debug
and FW 3.5


On 12 окт, 17:46, Krzysztof Koźmic <krzysztof.koz...@gmail.com> wrote:
>   yes, when I remove the call all works fine.
>
> On 12/10/2010 11:30 PM, Konstantin wrote:
>
>
>
> > Remove   tuneDynamicProxy() call and restrict creation of signed
> > assemby in some other way. Issue can be repeoduced where DP creates
> > only unsigned assembly.
> > The check for whether to create unsigned assembly or not is based on
> > SecurityPermissionFlag.UnmanagedCode check.
>
> > On 12 окт, 16:55, Krzysztof Koźmic<krzysztof.koz...@gmail.com>  wrote:
> >>    I ran it on .NET 4 under vs2010 and the code does not work at all.
>
> >> Interestingly - for completely different reasons:
>
> >> Unhandled Exception: System.TypeInitializationException: The type
> >> initializer for 'ConsoleApplication9.Program' threw an  exception. --->
> >> System.TypeInitializationException: The type initializer for
> >> 'Castle.DynamicProxy.Generators.Emitters.StrongNameUtil' threw an
> >> exception. --->  System.NotSupportedException: The Deny stack modifier
> >> has been obsoleted by the .NET Framework.  Please seehttp://go.microsoft.com/fwlink/?LinkId=155571formore information.

Konstantin

unread,
Oct 15, 2010, 9:14:57 AM10/15/10
to Castle Project Users

Krzysztof,

Can you advice anything regarding the issue?

Krzysztof Koźmic

unread,
Oct 15, 2010, 7:03:47 PM10/15/10
to castle-pro...@googlegroups.com
Konstantin,

I don't think there's anything I can do. I was able to reproduce the
issue, and it looks like the debugger in .NET 3.5 gets confused about
which generic parameters belong to which method.
DP generates perfect code, and it if you run PeVerfiy (I did) it finds
no issue.

Your best option is to upgrade to .NET 4 where this bug does not exist.

cheers,
Krzysztof

Reply all
Reply to author
Forward
0 new messages