Joseph
Joseph

Reputation: 963

PlatformNotSupportedException Ninject Xamarin.iOS

I have an iOS app, developed with Xamarin.iOS, that is using Ninject 3.3.0 for IoC. I am able to bind interfaces and implementations without issue, but I get a PlatformNotSupportedException on resolving those bindings with IResolutionRoot.Get<T>(). I am launching to a simulator on a connected Macbook. I have created a test (blank) iOS app to demonstrate the issue. Here are the relevant lines:

[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
  ...

  public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
  {
     ...

     var kernel = new StandardKernel();
     kernel.Bind<IFoo>().To<Foo>();

     var test = kernel.Get<IFoo>(); //exception thrown here

     return true;
  }
}

Here's the top of the stack trace (can provide more):

" at System.Reflection.Emit.DynamicMethod..ctor (System.String name, System.Type returnType, System.…"

According to this site, the kernel creates these DynamicMethod's for its bindings. Since Ninject is supported by .Net Standard 2.0, why am I getting this exception from such a simple operation?

Upvotes: 1

Views: 879

Answers (1)

Joseph
Joseph

Reputation: 963

Update: Portable.Ninject works, so use that instead if you're on iOS. The following is an explanation of how I got there and why regular Ninject doesn't work.

System.Reflection.Emit is not supported. Which likely means much of Ninject will not work for iOS.

These links were particularly misleading to me:

https://developer.xamarin.com/api/type/System.Reflection.Emit.DynamicMethod/

Having now read about this limitation, it's obvious the generation mentioned in this article impedes Ninject from working, though it's not explicitly stated.

http://arteksoftware.com/ioc-containers-with-xamarin/

The writer of this article mentions the limitation, though it appears he got it working.

Upvotes: 7

Related Questions