尝试将SimpleIOC用作ServiceLocator时抛出异常

时间:2018-04-19 08:55:47

标签: c# mvvm-light simpleioc

我的应用程序安装在原生的Win10 PC上,能够在问题上运行。 但是,当用户在Win7 / Win10 VM上安装应用程序时,他们会遇到以下堆栈跟踪崩溃:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at Services.CertService.RefreshCerts() in C:\Users\Services\CertService.cs:line 49
at Services.CertService..ctor(IAppSettings appSettings) in C:\Users\Services\CertService.cs:line 43
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at GalaSoft.MvvmLight.Ioc.SimpleIoc.MakeInstance[TClass]() in c:\MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras (PCL)\Ioc\SimpleIoc.cs:line 727
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at GalaSoft.MvvmLight.Ioc.SimpleIoc.DoGetService(Type serviceType, String key, Boolean cache) in c:\MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras (PCL)\Ioc\SimpleIoc.cs:line 567
at GalaSoft.MvvmLight.Ioc.SimpleIoc.GetInstance[TService]() in c:\MvvmLight\Source\GalaSoft.MvvmLight\GalaSoft.MvvmLight.Extras (PCL)\Ioc\SimpleIoc.cs:line 912
at WebServices.Login..ctor(Boolean signIn) in C:\Users\WebServices\Login.cs:line 69

和Login的类构造函数是:

69: protected Login(bool signIn = true)
70: {
71:     var service = ServiceLocator.Current.GetInstance<ICertService>();
72:     if (service != null)
73:     {
74:         if (signIn)
75:         {
76:             /* do something */
77:         }
78:         else
79:         {
80:             /* do something else */
81:         }
82:     }
83: }

在CertService类中,我们有以下内容:

34: public class CertService : ICertService
35: {
36:     private readonly IAppSettings appSettings;
37:     private Dictionary<string, string> pCerts;
38:     private List<string> hList;

40:     public CertService(IAppSettings appSettings)
41:     {
42:        this.appSettings = appSettings;
43:        this.RefreshCerts();
44:     }

46:     public void RefreshCerts()
47:     {
48:         this.pCerts = new Dictionary<string, string>();
49:         this.hList = new List<string>();
            ...
        }

我想我错过了我需要在我的VM上安装的依赖项。我不能认为这会是一个代码问题,因为它不会在本机Windows机器上发生。

0 个答案:

没有答案