2017-05-09 35 views
0

我正在使用MVVMCross的Xamarin應用程序。發佈模式中的IoCResolveException

當我在調試模式下運行時,該應用程序完美運行。

但是,如果嘗試在釋放模式運行失敗,異常:

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 

我使用MVVMCross IOC容器,我去註冊的依賴性如下:

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

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

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

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

     RegisterAppStart(new AppStart()); 
    } 

用戶服務等級:

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; 
    } 
    . 
    . 
    . 
} 

可能發生了什麼?

+0

如果您的鏈接器設置爲您的發佈版本設置爲「None」以外的其他值,請在您的UserService類上添加鏈接器[Preserve(AllMembers = true)],實際上所有的* Service和*存儲庫類... – SushiHangover

回答

1

您可以在您的PCL中創建一個PreserveAttribute,並將其添加到鏈接器正在剝離的類中。 Xamarin docs描述用作

如果你不想拿上Xamarin庫的依賴 - 爲 例如,你正在建設一個跨平臺的可移植類庫 (PCL) - 你仍然可以使用Android。 Runtime.Preserve屬性。要 做到這一點,聲明PreserveAttribute類Android.Runtime 命名空間內是這樣的:

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

如果你想阻止你的共享PCL的鏈接時,你可以使用link skip,以阻止鏈接器剝去代碼從你PCL。

在您的android cs proj中只需添加<AndroidLinkSkip>YourPCLAssemblyNameHerer</AndroidLinkSkip>或通過屬性UI。 Android選項 - >鏈接器 - >跳過鏈接程序集,在輸入中輸入您的PCL程序集名稱。