Bug 23437 - PCL Assembly TypeLoadException: Could not load type 'System.Object' from assembly 'System.Runtime
Summary: PCL Assembly TypeLoadException: Could not load type 'System.Object' from asse...
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: 3.8.0
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Alexander Köplinger [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2014-09-28 07:37 UTC by Johannes Rudolph
Modified: 2017-03-02 19:24 UTC (History)
6 users (show)

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


Attachments
Sample project reproducing the issue (6.72 MB, application/zip)
2014-09-28 07:37 UTC, Johannes Rudolph
Details

Description Johannes Rudolph 2014-09-28 07:37:14 UTC
Created attachment 8219 [details]
Sample project reproducing the issue

The attached project reproduces an issue in the following Scenario: 

* PCL projet (Profile 47) uses various standard Nuget packages (HttpClient, Async support etc.)
* NUnit test project (.NET 4.5) references and executes PCL, using NitoAsyncEx.AsyncContext (NitoAsyncEx is a PCL itself).

When running the test, I receive the following error: 

TypeLoadException: Could not load type 'System.Object' from assembly 'System.Runtime, Version=4.0.0.0, Culture...'
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00054] in /private/tmp/source-mono-mac-3.10.0-branch/bockbuild-mono-3.10.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.10.0/mcs/class/corlib/System.Reflection/MonoMethod.cs:230 


The compiler should under no circumstances emit a reference to System.Object in the System.Runtime assembly anywhere (it's a primitive type after all) and even then we should see it unified to the mscorlib type. So something's seriously wrong here... and I have a feeling that it is related to the PCL build in some way.
Comment 1 Johannes Rudolph 2014-09-28 08:31:52 UTC
I have taken a further look at the NitoAsyncEx assembly and narrowed it down that there is a problem with Mono executing PCL assemblies compiled for Profile 259. 

In particular, the NitoAsyncEx.dll referenced from the test project contains this (ildasm output):

.class public auto ansi sealed beforefieldinit Nito.AsyncEx.AsyncContext
       extends [System.Runtime]System.Object
       implements [System.Runtime]System.IDisposable
{

So what I assume happens is that the Mono runtime takes this literally and tries to resolve the System.Runtime.dll from the Library.Test/bin/Debug folder, where it actually finds one that is a PCL shim assembly from the Microsoft.Bcl package that was referenced by the Library project. Library.csproj contains

    </Reference>
    <Reference Include="System.Runtime">
      <HintPath>..\packages\Microsoft.Bcl.1.1.9\lib\portable-net40+sl5+win8+wp8+wpa81\System.Runtime.dll</HintPath>
    </Reference>

So to summarize: Mono has a problem executing assemblies compiled for Profile 259, when as shim System.Runtime.dll is present. 

I can't exactly say whether this is "by design" or a bug in the mono runtime. Will check what the MS CLR does in this case.

PCLs and properly building projects that have them makes my head hurt some times.
Comment 2 Rodrigo Kumpera 2014-11-12 10:15:39 UTC
Martin, any news on this bug?
Comment 3 Johannes Rudolph 2014-11-21 04:15:37 UTC
Any update? Just hit this issue again today in a different context. 

I can confirm this problem still persists in  mono --version
Mono JIT compiler version 3.10.0 ((detached/92c4884 Thu Nov 13 23:27:38 EST 2014)
Comment 4 Johannes Rudolph 2015-02-17 08:34:38 UTC
Any updates?
Comment 5 Toni 2015-06-13 03:41:09 UTC
Hi,

the same problem here, in this case using System.IO.Stream.

I do not include a reproducing project, but situation is as follows

    - a PCL library with a base class that uses System.IO.Stream.

    - a NUnit test project which derives the base class and also makes use of System.IO.Stream.

In all cases, everything compiles fins, but:

    - if the PCL library is set to profile 259, a TypeLoadException is thrown when executing the tests using NUnit, previous to any test execution, stating that the base class (from the PCL library) cannot be loaded  (both from within Xamarin Studio and invoking nunit-console).

    - if the PCL library is set to profile 78, no exception is thrown and the tests execute.

All this happens with following component versions, under Mac OS:

    - Mono JIT compiler version 4.0.1 ((detached/ed1d3ec Tue May 26 17:22:37 EDT 2015)

    - NUnit-Console version 2.6.4.14350

This also happens with NUnit 2.4.8 (which is shipped by default with this version of mono).
Comment 6 Charlie 2017-03-02 09:14:41 UTC
I'm still having this issue with NitoAsyncEx on Xamarin Studio 6.2 (build 1778) for Mac. Is a fix likely at any point in the future?

Note You need to log in before you can comment on or make changes to this bug.