Ninject issue on IOS

When I have property based injection and worse just add some property there, in the IOSNetworkInfo concrete class... I always get a crash "Object reference not set to instance of object" I googled for this exception but couldn't figure out my problem in the code

Here are my classes
public interface IService { void testme();}

  public class AndyServiceImplementation : IService

  public class IOSServiceImplementation : IService

  public class IOSNetworkInfo : INetworkInfo
        {
            [Inject, IOSEnvAttribute]
            public IService IOSService { get; set; }

            [Inject, AndEnvAttribute]
            public IService AndyService { get; set; }
        ........


public class dummyviewmodel
    {
        public INetworkInfo networkinfo;
        public dummyviewmodel (INetworkInfo s)
        {
            networkinfo = s;
        }
    }


//Getting dummy model
kernel.Get<dummyviewmodel>();


//Bindings
kernel.Bind<dummyviewmodel> ().ToSelf().InTransientScope();

//Bindings with attributes
kernel.Bind<IService> ().To<IOSService.IOSServiceImplementation> ().WhenMemberHas<IOSEnvAttribute> ().InTransientScope();
kernel.Bind<IService> ().To<AndyService.AndyServiceImplementation> ().WhenMemberHas<AndEnvAttribute>().InTransientScope();
kernel.Bind<INetworkInfo> ().To<IOSNetworkInfo> ().InTransientScope();

Posts

  • LeelaPrasannaKumarLeelaPrasannaKumar USUniversity

    Has anyone tried ninject on IOS already?

  • OysteinKrog_InitialForceOysteinKrog_InitialForce NOMember ✭✭
    edited October 2013

    Yeah I tried it but got linker errors..
    From what I understand Ninject/Autofac should now work due to the fact that they are both PCL and it's possible to use both without Reflection.Emit (Autofac does not have any emit code and ninject has an alternative resolution strategy that avoids emit).
    Unfortunately it seem things are still broken due to a bug?
    https://bugzilla.xamarin.com/show_bug.cgi?id=14765

    If anyone else knows anything about this, please chime in, I'm not 100% sure that I'm correct in saying that it should work (I know there are many limitations on IOS due to AOT linking, but I checked the source code for the relevant linq expressions and it seems to me that there are emit-less fallbacks in place that should be working for us here).

  • alexjhalexjh USMember
    edited January 2014

    Using the latest Ninject, I'm unable to instantiate the StandardKernel without getting a NotSupportedException thrown at runtime from the default constructor. No out-of-box support yet, I suppose.

  • OrenNovotnyOrenNovotny USMember, Insider, Beta, University ✭✭

    Autofac does not use Reflection.Emit, but it does use Expression.Compile(), which I believe is still on the no-go list for iOS.

    I haven't looked at the NInject code lately, so I'm not sure about that one.

  • AlexMertlichAlexMertlich USMember

    @alexjh I'm using the PCL version of ninject but I see their is a pre-processor directive defined to throw a NotImplementedException if called from the PCL.

    Have you been able to get ninject to work? I've noticed that Xamarin.Forms.Labs allows you to use ninject for DI but I've yet to find an example of it working?

  • SKallSKall USMember ✭✭✭✭

    Yes, unit tests confirm it is now working with 3.3 version. The important part is that the client project needs to install the NuGet package to each native platform.

Sign In or Register to comment.