"TypeInitialization Expected" thrown on Bootstrapper

Topics: Bootstrappers & IoC, Getting Started
Feb 26, 2013 at 10:34 AM
When I run my WPF app, I get a "TypeInitialization Expected" error as soon as I create a new Bootstrapper object.

Snippet of my "App.cs":
private readonly AppBootstrapper bootstrapper;
public App()
{
  this.bootstrapper = new AppBootstrapper(); // <--- Error gets thrown here
  this.bootstraper.InitializeLogger(); // logger 
  this.bootstrapper.Start(); 
}
Snippet of "AppBootstrapper.cs"
public class AppBootstrapper : Bootstrapper<IShellViewModel>
{
  // all of my code in here
}
I tried using the "ShellViewModel" instead of the interface and it still threw the exception. Is it throwing the exception because it doesn't know what view to use on startup?
Feb 26, 2013 at 10:43 AM
Edited Feb 26, 2013 at 10:44 AM
Have you exported an IShellViewModel/ShellViewModel (the export should match the type specified in the bootstrapper)?
Feb 26, 2013 at 11:09 AM
Yes. I added this at the top of ShellViewModel:
[Export(typeof(IShellViewModel))]
Do I need to add the above code to IShellViewModel as well?
Feb 26, 2013 at 11:12 AM
No, you need the export just on the relevant class that has to be instantiated by the IoC.
Could you provide the full exception message of the exception and it's inner exception if any? You can obtain the full message using the ToString method on the Exception object.
Feb 26, 2013 at 8:21 PM
I added "this.bootstrapper.ToString();" right after "this.bootstrapper = new AppBootstrapper();" and it still throws an exception at the "this.bootstrapper = new Appbootstrapper();" without doing the ToString().

Also, right below where it says "TypeInitializationException was unhandled" it gives a "The type initializer for 'MyApp.Helpers.AppBootstrapper' threw an exception."
Feb 26, 2013 at 8:30 PM
I meant the ToString of the exception. You can copy the full message looking at the exception details (if the debuggers prompts and shows the popup, you can press Show details and get all information you need, including stack trace and eventual inner exception).
Feb 26, 2013 at 8:53 PM
Edited Feb 26, 2013 at 8:54 PM
Edit: Not sure why it posted another one,I'll get that information here when I get home from work.
Feb 26, 2013 at 8:55 PM
I added "this.bootstrapper.ToString();" right after "this.bootstrapper = new AppBootstrapper();" and it still throws an exception at the "this.bootstrapper = new Appbootstrapper();" without doing the ToString().

Also, right below where it says "TypeInitializationException was unhandled" it gives a "The type initializer for 'MyApp.Helpers.AppBootstrapper' threw an exception."
Feb 26, 2013 at 8:55 PM
I added "this.bootstrapper.ToString();" right after "this.bootstrapper = new AppBootstrapper();" and it still throws an exception at the "this.bootstrapper = new Appbootstrapper();" without doing the ToString().

Also, right below where it says "TypeInitializationException was unhandled" it gives a "The type initializer for 'MyApp.Helpers.AppBootstrapper' threw an exception."
Feb 26, 2013 at 8:55 PM
Edited Feb 26, 2013 at 8:58 PM
Again... Sorry for the useless notification, it decided to post the exact same message every time I refreshed the page :(
Feb 26, 2013 at 9:02 PM
Here is the TypeInitializationException in full:
System.TypeInitializationException was unhandled
  HResult=-2146233036
  Message=The type initializer for 'MyDll.Helpers.AppBootstrapper' threw an exception.
  Source=MyDll
  TypeName=MyDll.Helpers.AppBootstrapper
  StackTrace:
       at MyDll.Helpers.AppBootstrapper..ctor()
       at MyDll.App..ctor() in c:\Users\Administrator\Documents\Visual Studio 2012\MyDll\MyDll\App.cs:line 37
       at MyDll.App.Main() in c:\Users\Administrator\Documents\Visual Studio 2012\MyDll\MyDll\App.cs:line 46
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.ArgumentNullException
       HResult=-2147467261
       Message=Value cannot be null.
Parameter name: operatingSystemInfo
       Source=MyDllDll
       ParamName=operatingSystemInfo
       StackTrace:
            at MyDll.Helpers.Ensure.ArgumentNotNull(Object value, String name) in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\MyDllDll\MyDllDll\Helpers\Ensure.cs:line 18
            at MyDll.IO.MyDllEnvironment..ctor(IOperatingSystemInfo operatingSystemInfo) in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\MyDllDll\MyDllDll\IO\MyDllEnvironment.cs:line 104
            at MyDll.Helpers.AppBootstrapper..cctor() in c:\Users\Administrator\Documents\Visual Studio 2012\MyDll\MyDll\Helpers\AppBootstrapper.cs:line 30
       InnerException: 
Feb 27, 2013 at 12:20 AM
That's not quite the whole exception - the InnerException is missing. A TypeInitializationException is just a wrapper exception, and the real exception is in the InnerException property. It looks as if it's going to be an ArgumentNullException. You can trap this by using the "Exceptions" dialog in Visual Studio and checking the ArgumentNullException in CLR Exceptions.
Feb 27, 2013 at 12:45 AM
I'm such a noob, what namespace is "ArgumentNotNull" under in the category "Common Language Runtime Exceptions"? I clicked on "Find" in the "Break when an exception is:" dialog box and typed in "ArgumentNotNull", but VS came back and said that it could not find that string. Thanks :)
Feb 27, 2013 at 12:57 AM
Should be System.ArgumentNullException.
Feb 27, 2013 at 3:15 AM
Thanks.. I got it to compile by commenting out the "ArgumentNotNull" so it only logs it and does not throw an exception. The problem is now everything compiles correctly, and when I press the green Start VS seems to launch the program, except when I open the Task Manager it does not show the program running. Any suggestions? Do you know where VS keeps the log when this occurs?