Bug 16720 - Reactive Extensions PCL NuGet packages cause "System.DllNotFoundException: kernel32.dll"
Summary: Reactive Extensions PCL NuGet packages cause "System.DllNotFoundException: ke...
Alias: None
Product: Visual Studio Extensions
Classification: Xamarin
Component: iOS ()
Version: 1.10.x
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Martin Baulig
Depends on:
Reported: 2013-12-10 23:19 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2015-07-29 11:17 UTC (History)
7 users (show)

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

Test case (15.77 KB, application/zip)
2013-12-10 23:19 UTC, Brendan Zagaeski (Xamarin Team, assistant)

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 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:

Description Brendan Zagaeski (Xamarin Team, assistant) 2013-12-10 23:19:30 UTC
Created attachment 5634 [details]
Test case

## Steps to reproduce

1. Open the attached test project (in Visual Studio 2013).

2. Build & run the project on the iOS simulator.

## Result

In the Debug output...

> Unhandled Exception:
> System.TypeInitializationException: An exception was thrown by the type initializer for System.Diagnostics.Stopwatch
> Unhandled Exception:
> System.TypeInitializationException: An exception was thrown by the type initializer for System.Diagnostics.Stopwatch ---> System.DllNotFoundException: kernel32.dll
>   at (wrapper managed-to-native) Microsoft.Win32.SafeNativeMethods:QueryPerformanceFrequency (long&)
>   at System.Diagnostics.Stopwatch..cctor () [0x00000] in <filename unknown>:0 

### Attempting to build for iPhone rather than iPhoneSimulator results in a linker error in the Build output

> stderr: error MT2001: Could not link assemblies. Reason: Argument cannot be null.
> Parameter name: key
> --- inner exception
> System.ArgumentNullException: Argument cannot be null.
> Parameter name: key
>   at System.Collections.Generic.Dictionary`2[Mono.Cecil.IMemberDefinition,System.Collections.Generic.List`1[Mono.Cecil.MethodDefinition]].TryGetValue (IMemberDefinition key, System.Collections.Generic.List`1& value) [0x00000] in <filename unknown>:0 
>   at Mono.Linker.AnnotationStore.GetPreservedMethods (IMemberDefinition definition) [0x00000] in <filename unknown>:0 
>   at Mono.Linker.AnnotationStore.AddPreservedMethod (IMemberDefinition definition, Mono.Cecil.MethodDefinition method) [0x00000] in <filename unknown>:0 
>   at Mono.Linker.AnnotationStore.AddPreservedMethod (Mono.Cecil.TypeDefinition type, Mono.Cecil.MethodDefinition method) [0x00000] in <filename unknown>:0 
>   at MonoTouch.Tuner.PreserveCode.PreserveQueryableEnumerable () [0x00000] in <filename unknown>:0 
>   at MonoTouch.Tuner.PreserveCode.Process (Mono.Linker.LinkContext context) [0x00000] in <filename unknown>:0 
>   at Mono.Linker.Pipeline.Process (Mono.Linker.LinkContext context) [0x00000] in <filename unknown>:0 
>   at MonoTouch.Tuner.Linker.Process (MonoTouch.Tuner.LinkerOptions options, Mono.Linker.LinkContext& context, System.Collections.Generic.List`1& assemblies) [0x00000] in <filename unknown>:0 
> ---
>   at MonoTouch.Tuner.Linker.Process (MonoTouch.Tuner.LinkerOptions options, Mono.Linker.LinkContext& context, System.Collections.Generic.List`1& assemblies) [0x00000] in <filename unknown>:0 
>   at MTouch.LinkAssemblies (System.String main, System.Collections.Generic.List`1& assemblies, System.String output_dir, Mono.Linker.LinkContext& link_context) [0x00000] in <filename unknown>:0 
>   at MonoTouch.Application.ManagedLink () [0x00000] in <filename unknown>:0 
>   at MonoTouch.Application.ProcessAssemblies () [0x00000] in <filename unknown>:0 
>   at MonoTouch.Application.BuildApp () [0x00000] in <filename unknown>:0 
>   at MonoTouch.Application.Build () [0x00000] in <filename unknown>:0 
>   at MTouch.Main2 (System.String[] args) [0x00000] in <filename unknown>:0 
>   at MTouch.Main (System.String[] args) [0x00000] in <filename unknown>:0 

## Additional information

Perhaps this is a bug in the Reactive Extensions NuGet packages themselves? Maybe one of them somehow references kernel32.dll when it shouldn't?

One "workaround" is to remove all of the NuGet packages, and instead use the "Reactive Extensions (Rx) for Xamarin" component.
After the project has been cleaned and rebuilt, the call to `System.Reactive.Concurrency.Scheduler.Schedule()` will complete successfully. A problem with this workaround is that if the app project then references a PCL project that in turn references the Reactive Extensions NuGet packages, the problem will return.

## Regression status

This test project shows the same behavior on Xamarin.iOS 1.8.

A little confusingly, the workaround of using the "Reactive Extensions (Rx) for Xamarin" component works "better" in Xamarin.iOS 1.8. One reason for this is that the N-th order references to the NuGet packages are not properly resolved in 1.8 (bug 16363), making it possible to mix together the two versions of the libraries.
Comment 4 Martin Baulig 2013-12-11 10:46:10 UTC
The Rx NuGet package contains different implementations for different target frameworks.  If you look inside the packages folder, you'll find multiple "packages/<Name>/lib/<framework>/Name.dll" for a bunch of different frameworks.

If you get an error about a missing kernel32.dll, then NuGet most likely references the wrong implementation.  This can be fixed by either manually editing the .csproj files and fixing the "HintPath"s or removing and re-adding the NuGet package (the NuGet package, not just the reference, there's a right-click option "Manage NuGet packages").
Comment 5 Martin Baulig 2013-12-11 11:18:36 UTC
Just downloaded your test case and compiled it, working fine for me after upgrading to Mono 3.2.6 (latest wrench build from the release lane) and my iOS hotfix (not committed to MonoTouch yet): https://www.dropbox.com/s/itfyfdwybrx8u13/Rx-iOS-Hotfix.zip
Comment 6 Brendan Zagaeski (Xamarin Team, assistant) 2013-12-17 16:37:08 UTC
Many thanks for the fix! Works nicely!
Comment 10 Rajneesh Kumar 2015-07-15 09:03:13 UTC
I have checked this issue with the latest stable build XVS 3.11.666 + XI and observed that this issue has been fixed and no longer exist with this build. I have used the attached test sample to check this issue and I am able to build and deploy on simulator successfully, without any error/exception. Please refer the screencast: http://www.screencast.com/t/eCz6V9RbLDYh

This issue has been fixed, hence I am closing this issue.


Environment Info:

Microsoft Visual Studio Professional 2013
Version 12.0.31101.00 Update 4
Microsoft .NET Framework
Version 4.5.50938
Installed Version: Professional
Xamarin   3.11.666.0 (ebae43a)
Visual Studio extension to enable development for Xamarin.iOS and
Xamarin.Android (5f55a9ef61c11b6ce0890bc91e4c71b1b92be214)
Visual Studio plugin to enable development for Xamarin.Android.
Xamarin.iOS (67b390d0d1bd741582cf7b7fd3ffea08583a3f83)
Visual Studio extension to enable development for Xamarin.iOS.
Comment 11 Daniel Cazzulino 2015-07-29 11:17:35 UTC
Closing per last comment.