Using RoleEnvironment in a console app in .NET4

If you are writing a console app in .NET4 and want to use the RoleEnvironment then you’ll get an error:

The type initializer for ‘Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment’ threw an exception.

To get around this, just add a “useLegacyV2RuntimeActivationPolicy” to the startup tag generated in the default app.config:

 <startup useLegacyV2RuntimeActivationPolicy=”true”>

 This is because Microsoft.WindowsAzure.ServiceRuntime.dll is a mixed mode assembly. The useLegacyV2RuntimeActivationPolicy attribute is required for referencing any mixed mode assembly, not only the Windows Azure ones.