Bug 10755 - Mono 3.0.5 crashes on portable class libraries with extension methods
Summary: Mono 3.0.5 crashes on portable class libraries with extension methods
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: unspecified
Hardware: Other Linux
: --- normal
Target Milestone: ---
Assignee: Martin Baulig
URL:
Depends on:
Blocks:
 
Reported: 2013-02-28 02:46 UTC by Erik Schierboom
Modified: 2013-07-01 05:47 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Erik Schierboom 2013-02-28 02:46:25 UTC
The Mono 3.0.5 runtime crashes when an application uses an extension method defined in a portable class library. If you run such an application, it crashes with the following error:
Missing method .ctor in assembly /var/www/sites/erik/monoconsole/PortableClassLibraryWithExtensionMethod.dll, type System.Runtime.CompilerServices.ExtensionAttribute
Can't find custom attr constructor image: /var/www/sites/erik/monoconsole/PortableClassLibraryWithExtensionMethod.dll mtoken: 0x0a000010

Unhandled Exception:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies.
File name: 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'
  at ConsoleApplicationWithExtensionMethod.Program.<Main>b__0 (System.Type t) [0x00000] in <filename unknown>:0
  at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator28`1[System.Type].MoveNext () [0x00000] in <filename unknown>:0
  at System.Linq.Enumerable.Count[Type] (IEnumerable`1 source) [0x00000] in <filename unknown>:0
  at ConsoleApplicationWithExtensionMethod.Program.Main () [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.FileNotFoundException: Could not load file or assembly 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies.
File name: 'System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'
  at ConsoleApplicationWithExtensionMethod.Program.<Main>b__0 (System.Type t) [0x00000] in <filename unknown>:0
  at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator28`1[System.Type].MoveNext () [0x00000] in <filename unknown>:0
  at System.Linq.Enumerable.Count[Type] (IEnumerable`1 source) [0x00000] in <filename unknown>:0
  at ConsoleApplicationWithExtensionMethod.Program.Main () [0x00000] in <filename unknown>:0


The thing that immediately struck me was this line: 
Missing method .ctor in assembly /var/www/sites/erik/monoconsole/PortableClassLibraryWithExtensionMethod.dll, type System.Runtime.CompilerServices.ExtensionAttribute

It looks like Mono is not able to find the ExtensionAttribute class. After some searching, I found that this is probably due to the ExtensionAttribute class being moved to another library in the .NET 4.5 in-place upgrade. On .NET 4.0 it was contained in System.Core  but on .NET 4.5 it is contained in mscorlib (see http://www.mattwrock.com/post/2012/02/29/What-you-should-know-about-running-ILMerge-on-Net-45-Beta-assemblies-targeting-Net-40.aspx)

I have created a repro of this problem at: https://github.com/ErikSchierboom/monoportableclasslibraryextensionmethod It contains two console applications and two portable class libraries. One portable class library defines a method as an extension method and the other does not. One console application uses the extension method version, and the other does not. To verify the problem, run both console applications on a Mono 3.0.5 environment. The one that uses the extension method version will crash and the other will not.
Comment 1 Rodrigo Kumpera 2013-05-16 22:36:54 UTC
Martin, could you test this with your PCL work enabled?
Comment 2 Martin Baulig 2013-05-24 15:48:52 UTC
Where does this Profile136 in your test case come from?  This does not exist on my machine, running Windows 7 and VS2k12.

However, extension methods in PCL libraries should work with Mono.  I'll quickly write a small test myself to verify this.
Comment 3 Erik Schierboom 2013-05-25 03:32:51 UTC
Martin, I just selected the portable class library option in Visual Studio.
Comment 4 Martin Baulig 2013-05-25 10:08:35 UTC
Which version of Visual Studio and Windows are you using?  Did you install any Nuget packages that provide additional target frameworks?

What's in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.0\Profile?  You should have the following directories in there:

Profile1, Profile104, Profile131, Profile14, Profile18, Profile19 ,Profile2, Profile23, Profile24
Profile3, Profile36, Profile37, Profile4, Profile41, Profile42, Profile46, Profile47, Profile5, Profile6
Profile88, Profile95, Profile96

Do these exist or do you have completely different numbers?  These profile names are written into the .csproj file; yours has
    <TargetFrameworkProfile>Profile136</TargetFrameworkProfile>

This does not exist on a normal Windows 7 / VS2k12 computer, so you won't be able to use your PCL project there.
Comment 5 Erik Schierboom 2013-05-26 04:52:22 UTC
On my machine, tThat directory does contain a folder for Profile136. I am developing on a Windows 8 / VS2K12 machine. Perhaps it has something to do with the fact that I have the Windows Phone 8 SDK installed? If I look into the SupportedFramework directory within the Profile136 directory it lists the following files:
.NET for Windows Store apps.xml
.NET Framework 4.xml
Silverlight 5.xml
Windows Phone 8.xml

Do you need more information?
Comment 6 Martin Baulig 2013-06-10 13:23:47 UTC
We won't support Windows Phone 8 in the upcoming PCL release, so you'd need to select a different profile.  In Visual Studio, right-click your project, go Properties and then select a combination of profiles that do not include Windows Phone 8

Tested and confirmed working with https://github.com/baulig/animated-octo-nemesis/tree/master/samples/ExtensionMethod.
Comment 7 Erik Schierboom 2013-07-01 05:47:16 UTC
I can confirm that the latest Mono version does indeed solve this problem.