Cannot load Exchange powershell snap-in: The type initializer for 'Microsoft.Exchange.Data.Directory.Globals' threw an exception
After further investigation I figured out that .NET 4.5 is an in place update meaning that .NET 4.0 is overwritten and replace with .NET 4.5 when installed. I don't know what changed in .NET 4.5 that causes this but the issue is resolved by uninstalling .NET 4.5 and switching back to Visual Studio 2010. Hopefully Microsoft will have some update in the near future that will resolve the issue and allow me to use Visual Studio 2012 again.
See the following article for more info about the in place update. http://www.devproconnections.com/article/net-framework/net-framework-45-versioning-faces-problems-141160
I have investigated the actual bug in the Microsoft Exchange assemblies, and the problem was the ExTraceConfiguration class (internal) from the Microsoft.Exchange.Diagnostics.dll assembly enumerates all loaded assemblies in the current app domain. If it finds System.IdentityModel or System.ServiceModel, it uses reflection to configure some tracing for them. But the reflection code is not compatible with the .net 4.5 ServiceModel and an error occurs. After the error is detected (a null condition is checked) a trace is tried, but the code is currently in the process of configuring tracing so => hard crash.
The solution is to use reflection on the Microsoft.Exchange.Diagnostics.dll, load up the ExTraceConfiguration type and run it's type initializer:
type.TypeInitializer.Invoke(null, null);
before System.ServiceModel has had a chance to load yet in your app domain. This initializer is a static constructor which can only run once per process, so after that you are safe to load ServiceModel if you need it.
It appear to be a know bug. There is a Microsoft connect report:
https://connect.microsoft.com/VisualStudio/feedback/details/770748/powershell-exception-after-4-5-upgrade#tabs
Microsoft's response is that they have "logged [the] issue with the Exchange team"
As a workaround you can do one of the following:
- Uninstall .NET 4.5
- Change the your application's target framework to 2.0 or 3.5.