[Mono-dev] Strange Casting bug in .net 4 profile

Gary Thomas Gary.Thomas at ioko.com
Thu Aug 19 05:50:04 EDT 2010


I tried the app.config below and this fixed the casting issue but caused the application to crash on exit.

I think I found the root cause and it is with the machine.config file in /opt/mono/etc/mono/4.0/machine.config
The configSections element in there had a sectionGroup for system.ServiceModel with references to the 3.0.0.0 version of System.ServiceModel. Changing these references to 4.0.0.0 fixes the problem.

I am happy to contribute a patch for this file if someone wants to review and commit it. I am also happy to raise a bug report if that helps the process along.

Thanks,
Gary

-----Original Message-----
From: mono-devel-list-bounces at lists.ximian.com [mailto:mono-devel-list-bounces at lists.ximian.com] On Behalf Of Robert Jordan
Sent: 18 August 2010 17:36
To: mono-devel-list at lists.ximian.com
Subject: Re: [Mono-dev] Strange Casting bug in .net 4 profile

On 18.08.2010 17:05, Gary Thomas wrote:
> A little more digging using reflector on the binary shows the path to the dependency as follows:
>
> CastBug 0.0.0.0 depends on
> System.ServiceModel 4.0.0 which depends on
> Microsoft.Transactions.Bridge 3.0.0 which depends on
> System.ServiceModel 3.0.0.0

Try to remap System.ServiceModel 3.0 to 4.0 using an app.config:

<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
	<assemblyIdentity
		name="System.ServiceModel"
		publicKeyToken="b77a5c561934e0895"
	/>
	<bindingRedirect
		oldVersion="3.0.0.0"
		newVersion="4.0.0.0"
	/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>



>
>
>
> -----Original Message-----
> From: mono-devel-list-bounces at lists.ximian.com [mailto:mono-devel-list-bounces at lists.ximian.com] On Behalf Of Gary Thomas
> Sent: 18 August 2010 15:42
> To: Robert Jordan; mono-devel-list at lists.ximian.com
> Subject: Re: [Mono-dev] Strange Casting bug in .net 4 profile
>
> There are some similar looking bugs on bugzilla but it is still happening in trunk now.
>
> I just deleted /opt/mono
> ran:
> make clean
> git pull
> ./autogen.sh --prefix=/opt/mono
> make
> sudo make install
>
> then recompiled the test app, ran it and still get the same results.
>
> Below is a trace of the application run with --debug. It seems to show that System.Configuration 2.0.0.0 assembly load is redirected to 4.0.0.0
> However I see that two versions of System.ServiceModel are being loaded (the class I am trying to cast to is in System.ServiceModel) so perhaps this is the problem after all.
> Any idea how to fix? Is it a binding redirect in the mono config file or something simple like that?
>
> Thanks,
> Gary
>
> [mono] ~/Projects/CastBug @ MONO_LOG_LEVEL=debug MONO_LOG_MASK=asm mono --debug CastBug.exe
> Mono: Assembly Loader probing location: '/opt/mono/lib/mono/4.0/mscorlib.dll'.
> Mono: Image addref mscorlib 0x86f5088 ->  /opt/mono/lib/mono/4.0/mscorlib.dll 0x86f4710: 2
>
> Mono: Assembly Loader loaded assembly from location: '/opt/mono/lib/mono/4.0/mscorlib.dll'.
> Mono: Assembly Ref addref mscorlib 0x86f5088 ->  mscorlib 0x86f5088: 1
>
> Mono: Assembly mscorlib 0x86f5088 added to domain CastBug.exe, ref_count=2
>
> Mono: Assembly Loader probing location: 'CastBug.exe'.
> Mono: Image addref CastBug 0x87443a0 ->  /home/garyt/Projects/CastBug/CastBug.exe 0x86f3c00: 3
>
> Mono: Assembly CastBug 0x87443a0 added to domain CastBug.exe, ref_count=1
>
> Mono: Assembly Loader loaded assembly from location: 'CastBug.exe'.
> Mono: Assembly Loader probing location: 'CastBug.exe'.
> Mono: Assembly Ref addref CastBug 0x87443a0 ->  mscorlib 0x86f5088: 3
>
> Mono: Assembly Loader probing location: '/opt/mono/lib/mono/gac/System.ServiceModel/4.0.0.0__b77a5c561934e089/System.ServiceModel.dll'.
> Mono: Image addref System.ServiceModel 0x874c5d8 ->  /opt/mono/lib/mono/gac/System.ServiceModel/4.0.0.0__b77a5c561934e089/System.ServiceModel.dll 0x874bd20: 2
>
> Mono: Assembly System.ServiceModel 0x874c5d8 added to domain CastBug.exe, ref_count=1
>
> Mono: Assembly Loader loaded assembly from location: '/opt/mono/lib/mono/gac/System.ServiceModel/4.0.0.0__b77a5c561934e089/System.ServiceModel.dll'.
> Mono: Assembly Ref addref CastBug 0x87443a0 ->  System.ServiceModel 0x874c5d8: 2
>
> Mono: Assembly Loader probing location: '/opt/mono/lib/mono/gac/System.Configuration/4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll'.
> Mono: Image addref System.Configuration 0x87579d8 ->  /opt/mono/lib/mono/gac/System.Configuration/4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll 0x8757020: 2
>
> Mono: Assembly System.Configuration 0x87579d8 added to domain CastBug.exe, ref_count=1
>
> Mono: Assembly Loader loaded assembly from location: '/opt/mono/lib/mono/gac/System.Configuration/4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll'.
> Mono: Assembly Ref addref System.ServiceModel 0x874c5d8 ->  System.Configuration 0x87579d8: 2
>
> Mono: Assembly Ref addref System.Configuration 0x87579d8 ->  mscorlib 0x86f5088: 4
>
> Mono: Assembly Ref addref CastBug 0x87443a0 ->  System.Configuration 0x87579d8: 3
>
> Mono: Assembly Loader probing location: '/opt/mono/lib/mono/gac/System/4.0.0.0__b77a5c561934e089/System.dll'.
> Mono: Image addref System 0x87615f8 ->  /opt/mono/lib/mono/gac/System/4.0.0.0__b77a5c561934e089/System.dll 0x8760c10: 2
>
> Mono: Assembly System 0x87615f8 added to domain CastBug.exe, ref_count=1
>
> Mono: Assembly Loader loaded assembly from location: '/opt/mono/lib/mono/gac/System/4.0.0.0__b77a5c561934e089/System.dll'.
> Mono: Assembly Ref addref System.Configuration 0x87579d8 ->  System 0x87615f8: 2
>
> Mono: Assembly Loader probing location: '/opt/mono/lib/mono/gac/System.Xml/4.0.0.0__b77a5c561934e089/System.Xml.dll'.
> Mono: Image addref System.Xml 0x876fbb0 ->  /opt/mono/lib/mono/gac/System.Xml/4.0.0.0__b77a5c561934e089/System.Xml.dll 0x876f5c0: 2
>
> Mono: Assembly System.Xml 0x876fbb0 added to domain CastBug.exe, ref_count=1
>
> Mono: Assembly Loader loaded assembly from location: '/opt/mono/lib/mono/gac/System.Xml/4.0.0.0__b77a5c561934e089/System.Xml.dll'.
> Mono: Assembly Ref addref System.Configuration 0x87579d8 ->  System.Xml 0x876fbb0: 2
>
> Mono: Assembly Ref addref System.Xml 0x876fbb0 ->  mscorlib 0x86f5088: 5
>
> Mono: Assembly Ref addref System 0x87615f8 ->  mscorlib 0x86f5088: 6
>
> Mono: Assembly Ref addref System.Xml 0x876fbb0 ->  System 0x87615f8: 3
>
> Mono: Assembly Loader probing location: '/opt/mono/lib/mono/gac/System.ServiceModel/3.0.0.0__b77a5c561934e089/System.ServiceModel.dll'.
> Mono: Image addref System.ServiceModel 0x8834158 ->  /opt/mono/lib/mono/gac/System.ServiceModel/3.0.0.0__b77a5c561934e089/System.ServiceModel.dll 0x88300d0: 2
>
> Mono: Assembly System.ServiceModel 0x8834158 added to domain CastBug.exe, ref_count=1
>
> Mono: Assembly Loader loaded assembly from location: '/opt/mono/lib/mono/gac/System.ServiceModel/3.0.0.0__b77a5c561934e089/System.ServiceModel.dll'.
> Mono: The request to load the assembly System.Configuration v2.0.0.0 was remapped to v4.0.0.0
> Mono: Assembly Ref addref System.ServiceModel 0x8834158 ->  System.Configuration 0x87579d8: 4
>
> Mono: The request to load the assembly mscorlib v2.0.0.0 was remapped to v4.0.0.0
> Mono: Assembly Ref addref System.ServiceModel 0x8834158 ->  mscorlib 0x86f5088: 7
>
> Mono: Assembly Ref addref System 0x87615f8 ->  System.Xml 0x876fbb0: 3
>
> System.ServiceModel.Configuration.ServicesSection
> section is ConfigurationSection True
> section is ServicesSection False
> ServiceSection type System.ServiceModel.Configuration.ServicesSection
> Casting section
>
> Unhandled Exception: System.InvalidCastException: Cannot cast from source type to destination type.
>    at CastBug.MainClass.Main (System.String[] args) [0x0006e] in /home/garyt/Projects/CastBug/CastBug.cs:19
> [mono] ~/Projects/CastBug @
>
>
> -----Original Message-----
> From: mono-devel-list-bounces at lists.ximian.com [mailto:mono-devel-list-bounces at lists.ximian.com] On Behalf Of Robert Jordan
> Sent: 18 August 2010 13:29
> To: mono-devel-list at lists.ximian.com
> Subject: Re: [Mono-dev] Strange Casting bug in .net 4 profile
>
> Hey,
>
> I believe this is already fixed in trunk. It was a mismatch
> between the 2.0 and 4.0 System.Configuration.dll that were
> loaded together into the same appdomain. This means that
> ServicesSection defined in these assemblies were actually
> incompatible to each other.
>
> Robert
>
> On 18.08.2010 13:52, Gary Thomas wrote:
>> Hi all,
>> 	I have been using mono from trunk and playing with the WCF stuff when I noticed an InvalidCastException being thrown. I figured out that this only happened when using the .net 4 profile, when using .net 3.5 profile it didn't happen.
>> I have put together a simple app to show the problem and here are the results.
>>
>> With .net 4:
>>
>> [mono] ~/Projects/CastBug @ dmcs CastBug.cs /reference:System.Configuration /reference:System.ServiceModel
>> CastBug.cs(19,41): warning CS0219: The variable `ss' is assigned but its value is never used
>> Compilation succeeded - 1 warning(s)
>> [mono] ~/Projects/CastBug @ mono CastBug.exe
>> System.ServiceModel.Configuration.ServicesSection
>> section is ConfigurationSection True
>> section is ServicesSection False
>> ServiceSection type System.ServiceModel.Configuration.ServicesSection
>> Casting section
>>
>> Unhandled Exception: System.InvalidCastException: Cannot cast from source type to destination type.
>>     at CastBug.MainClass.Main (System.String[] args) [0x00000] in<filename unknown>:0
>>
>>
>> With .net 3.5:
>>
>> [mono] ~/Projects/CastBug @ gmcs CastBug.cs /reference:System.Configuration /reference:System.ServiceModel
>> CastBug.cs(19,41): warning CS0219: The variable `ss' is assigned but its value is never used
>> Compilation succeeded - 1 warning(s)
>> [mono] ~/Projects/CastBug @ mono CastBug.exe
>> System.ServiceModel.Configuration.ServicesSection
>> section is ConfigurationSection True
>> section is ServicesSection True
>> ServiceSection type System.ServiceModel.Configuration.ServicesSection
>> Casting section
>> Done casting section
>>
>> The runtime reports the type of the object as ServicesSection but casting it to a ServicesSection causes an InvalidCastException.
>>
>> Here is the test app:
>>
>> using System;
>> using System.Configuration;
>> using System.ServiceModel.Configuration;
>>
>> namespace CastBug
>> {
>>           class MainClass
>>           {
>>                   public static void Main (string[] args)
>>                   {
>>                           var section = ConfigurationManager.GetSection("system.serviceModel/services");
>>                           Console.WriteLine(section.GetType().ToString());
>>                           Console.WriteLine("section is ConfigurationSection {0}", section is ConfigurationSection);
>>                           Console.WriteLine("section is ServicesSection {0}", section is ServicesSection);
>>                           Console.WriteLine("ServiceSection type {0}", typeof(ServicesSection).ToString());
>>
>>                           Console.WriteLine("Casting section");
>>                           // throws an InvalidCastException
>>                           ServicesSection ss = (ServicesSection)section;
>>                           Console.WriteLine("Done casting section");
>>
>>                   }
>>           }
>> }
>>
>> Is this a known issue? Should I report a bug? If it was a simple bug in the c# libraries I would have a go at fixing myself but this seem more fundamental than that.
>>
>> Thanks,
>> Gary
>
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list


_______________________________________________
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list


More information about the Mono-devel-list mailing list