1

My app installs on a native Win10 PC and is able to run with on issues. However, when a user installs the app on a Win7/Win10 VM, the they encounter a crash with the following stack trace:

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

and the class constructor for Login is:

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: }

Inside the CertService class we have the following:

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

I think I am missing a dependency that I need to install on my VM. I can't think that this would be a code issue since it doesn't happen on a native Windows machine.

  • It is good that you provide the stack trace because it helps understanding the problem. However, you have not provided information about the exception being thrown which is also important. – Martin Liversage Apr 19 '18 at 08:58
  • 2
    That's not a crash, it's not related to VMs or the Windows version. It's a bug in your code. Post the *FULL* exception, including its call stack. You can get it easily with `Exception.ToString()`. Something tells me the exception message would say that you haven't registered `CertService` – Panagiotis Kanavos Apr 19 '18 at 08:59
  • BTW the code you tried is an **anti**pattern. Instead of using dependency injection you tried to grab the DI container itself. Dependency injectino means that your *method* or *class* will receive the `CertService` instance from *somewhere* without knowing or caring how. No ServiceLocator calls anywhere. The *container* should create the class itself and pass any required CertService instances to its constructor – Panagiotis Kanavos Apr 19 '18 at 09:02
  • Your constructor should be `protected Login(CertService service,bool signIn = true){...}`. Your derived classes should also have a `CertService` parameter which they'll pass to the base `Login` constructor. BTW a protected constructor means your class can only be used as a base class for other implementations. – Panagiotis Kanavos Apr 19 '18 at 09:05
  • The `CertService` constructor throws for one reason or the other. Read the exception. – CodeCaster Apr 19 '18 at 09:07
  • Thanks. Let me try to get the full exception info. This is not my code, but I have to debug it. – just_another_engineer Apr 19 '18 at 09:12
  • The ServiceLocator was supposed to use ICertService, which I have updated, but there is still a an exception. We do register ICertService as `SimpleIoc.Default.Register();`. I've added the full exception info and code snippets of the affected calls. – just_another_engineer Apr 19 '18 at 10:39
  • The method `CertService.RefreshCerts()` throws a [NullReferenceException](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) for some reason. The culprit is in `...`. We can't reproduce this without relevant code. Read [ask] and provice a [mcve]. – CodeCaster Apr 19 '18 at 10:53

0 Answers0