Elton Santana
Elton Santana

Reputation: 989

IoCResolveException in release mode

I'm working in a Xamarin app with MVVMCross.

The app works perfectly when I run in debug mode.

But if try to run in release mode it fails with the exception:

Exception masked MvxIoCResolveException: Failed to resolve type 
FlexConta.Contracts.Service.IUserService
[mvx]     at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.Resolve (System.Type t) [0x00035] in <0da3cbd163cf47a29ec04fff5bb9eecd>:0 
[mvx]   at MvvmCross.Platform.IoC.MvxSimpleIoCContainer.Resolve[T] () [0x00000] in <0da3cbd163cf47a29ec04fff5bb9eecd>:0 
[mvx]   at MvvmCross.Platform.Mvx.Resolve[TService] () [0x00005] in <0da3cbd163cf47a29ec04fff5bb9eecd>:0 
[mvx]   at FlexConta.Core.AppStart.Start (System.Object hint) [0x00000] in <880d0bdc2a5448ffb4d7b35d827753b5>:0 
[mvx]   at MvvmCross.Droid.Support.V7.AppCompat.MvxSplashScreenAppCompatActivity.TriggerFirstNavigate () [0x00005] in <74631770bbbe4bff8d50c85acb55083c>:0 
[mvx]   at MvvmCross.Droid.Support.V7.AppCompat.MvxSplashScreenAppCompatActivity.InitializationComplete () [0x00009] in <74631770bbbe4bff8d50c85acb55083c>:0 
[mvx]   at MvvmCross.Droid.Platform.MvxAndroidSetupSingleton.<InitializeFromSplashScreen>b__7_1 () [0x0000a] in <099dd6f64bd74189922e6888bc76e146>:0 
[mvx]   at MvvmCross.Platform.Core.MvxMainThreadDispatcher.ExceptionMaskedAction (System.Action action) [0x00000] in <0da3cbd163cf47a29ec04fff5bb9eecd>:0 

I'm using the MVVMCross IOC container and I'm registering de dependencies as follows:

    public override void Initialize()
    {
        base.Initialize();

        CreatableTypes()
            .EndingWith("Repository")
            .AsInterfaces()
            .RegisterAsLazySingleton();

        CreatableTypes()
            .EndingWith("Service")
            .AsInterfaces()
            .RegisterAsLazySingleton();

        Mvx.RegisterSingleton<IUserRestAPI>(new UserRestAPI());

        RegisterAppStart(new AppStart());
    }

The User service class:

public class UserService : IUserService
{
    private readonly IUserRepository _userRepository;
    private readonly IDocumentTypesRepository _documentTypesRepository;
    private readonly IUserRestAPI _userRestAPI;

    public UserService(IUserRepository userRepository, IDocumentTypesRepository documentTypesRepository, IUserRestAPI userRestAPI)
    {
        _userRepository = userRepository;
        _documentTypesRepository = documentTypesRepository;
        _userRestAPI = userRestAPI;
    }
    .
    .
    .
}

What may be happening?

Upvotes: 0

Views: 170

Answers (1)

Plac3Hold3r
Plac3Hold3r

Reputation: 5182

You can create a PreserveAttribute in your PCL and add it to the classes that the linker is stripping out. Xamarin docs describe the use as

If you do not want to take a dependency on the Xamarin libraries – for example, you are building a cross platform portable class library (PCL) – you can still use the Android.Runtime.Preserve attribute. To do this, declare a PreserveAttribute class within the Android.Runtime namespace like this:

namespace Android.Runtime
{
    public sealed class PreserveAttribute : System.Attribute
    {
        public bool AllMembers;
        public bool Conditional;
    }
}

If you want to prevent linking of your shared PCL you can use link skip to prevent the linker from stripping away code from you PCL.

In your android cs proj just add <AndroidLinkSkip>YourPCLAssemblyNameHerer</AndroidLinkSkip> or via the properties UI. Android Options -> Linker -> Skip linking assemblies, enter your PCL assembly name in the input.

Upvotes: 1

Related Questions