Bug 14863 - Simple TinyIoC setup stops to work when linker is set to link SDK only.
Summary: Simple TinyIoC setup stops to work when linker is set to link SDK only.
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 7.0.0.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
Depends on:
Reported: 2013-09-19 13:15 UTC by Vratislav Kalenda
Modified: 2014-08-25 12:13 UTC (History)
3 users (show)

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

Project demonstrating the issue (32.62 KB, application/zip)
2013-09-19 13:15 UTC, Vratislav Kalenda

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 or GitHub 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 Vratislav Kalenda 2013-09-19 13:15:08 UTC
When deploying to iPhone 5 with iO7 with linker set to "Link SDK assemblies only" the following code in App delegate does not work.

		public override bool FinishedLaunching (UIApplication app, NSDictionary options)
			// create a new window instance based on the screen size
			window = new UIWindow (UIScreen.MainScreen.Bounds);

			var container = TinyIoC.TinyIoCContainer.Current;
			container.Resolve<ISampleInterface> (); //Throws TinyIoC.TinyIoCResolutionException: "Unable to resolve type: ioctest.ios.InterfaceImplementation"

When the linker is set to "Don't link", the code runs fine. Both the interface and its implementation is declared in same assembly. I have attached test project (based on TabBar iPhone application project template) which shows this issue.

My Xamarin configuration:
Xamarin Studio
Version 4.0.12 (build 3)
Installation UUID: 2723eaa9-eccf-4d7b-b8f9-a74c7954f5fe
	Mono 3.2.3 ((no/8d3b4b7)
	GTK 2.24.20
	GTK# (
	Package version: 302030000

Apple Developer Tools
Xcode 5.0 (3332.25)
Build 5A1413

Xamarin.Mac: Not Installed

Version: 4.8.1 (Trial Edition)
Android SDK: /Users/Kal/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
Java SDK: /usr
java version "1.6.0_51"
Java(TM) SE Runtime Environment (build 1.6.0_51-b11-457-11M4509)
Java HotSpot(TM) 64-Bit Server VM (build 20.51-b01-457, mixed mode)

Version: (Trial Edition)
Hash: aa35ba3
Build date: 2013-18-09 16:42:48-0400

Build Information
Release ID: 400120003
Git revision: 593d7acb1cb78ceeeb482d5133cf1fe514467e39
Build date: 2013-08-07 20:30:53+0000
Xamarin addins: 25a0858b281923e666b09259ad4746b774e0a873

Operating System
Mac OS X 10.8.5
Darwin mckal 12.5.0 Darwin Kernel Version 12.5.0
    Mon Jul 29 16:33:49 PDT 2013
    root:xnu-2050.48.11~1/RELEASE_X86_64 x86_64
Comment 1 Vratislav Kalenda 2013-09-19 13:15:50 UTC
Created attachment 4930 [details]
Project demonstrating the issue
Comment 2 Sebastien Pouliot 2013-09-19 15:03:08 UTC
The real, inner exception, is:

{System.NullReferenceException: Object reference not set to an instance of an object
  at System.Linq.Expressions.Expression.CreateExpressionOf (System.Type type, System.Linq.Expressions.Expression body, System.Collections.ObjectModel.ReadOnlyCollection`1 parameters) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq.Expressions/Expression.cs:1585 
  at System.Linq.Expressions.Expression.Lambda (System.Type delegateType, System.Linq.Expressions.Expression body, IEnumerable`1 parameters) [0x00039] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq.Expressions/Expression.cs:1600 
  at System.Linq.Expressions.Expression.Lambda (System.Type delegateType, System.Linq.Expressions.Expression body, System.Linq.Expressions.ParameterExpression[] parameters) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System.Linq.Expressions/Expression.cs:1580 
  at TinyIoC.TinyIoCContainer.CreateObjectConstructionDelegateWithCache (System.Reflection.ConstructorInfo constructor) [0x000a3] in /Users/sebastienpouliot/Downloads/ioctest/ioctest.ios/TinyIoC.cs:3701 
  at TinyIoC.TinyIoCContainer.ConstructType (System.Type requestedType, System.Type implementationType, System.Reflection.ConstructorInfo constructor, TinyIoC.NamedParameterOverloads parameters, TinyIoC.ResolveOptions options) [0x00120] in /Users/sebastienpouliot/Downloads/ioctest/ioctest.ios/TinyIoC.cs:3665 }

That's being retrown by TinyIoC.cs
Comment 3 Sebastien Pouliot 2013-09-19 15:37:59 UTC
Out of curiousity did this work before the 7.0 release (is it a regression) ? if so which version did you use before ?

Or is this a new application / code ?
Comment 4 Sebastien Pouliot 2013-09-19 16:04:42 UTC
It's a bug because Expression uses reflection to access the internal constructor of Expression<TDelegate>

Since it's a reflection usage the linker needs to know about it (and right now it does not).

The workaround (for any existing version of Xamarin.iOS) is to add "-linkskip=System.Core" to your "Additional mtouch arguments", inside your Project Options, iOS Build.
Comment 5 Vratislav Kalenda 2013-09-19 16:15:47 UTC
We have one project that was created and built against Xamarin.iOS Alpha release just before the official one. Everything concerning IoC works in that project even no with the current Xamarin.iOS release.
Thanks for the workaround. Greatly appreciated.
Comment 6 Sebastien Pouliot 2013-09-20 08:41:15 UTC
Fixed ios7 622761580aa106187f9a91f55addaba4e366a863

Note: it does not commonly fail because the ctor is used internally (without reflection) inside Expression. So it's missing only if that part of Expression is unused (and removed by the linker). That happens to be the case with TinyIoC