The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception
Asked Answered
B

3

7

I am trying to use Windows Azure Caching to store sessions in a MVC4 application. I build a application following steps from Link but when i try to make object of DataCache using below code line.

DataCache cache = new DataCache("default");

Errors occurs:

Microsoft.WindowsAzure.ServiceRuntime.dll not found or version mismatched, I updated my Windows Azure Emulator version to 2.0.0 and install WindowsAzure.Caching package version 2.0.0.0 using NuGet package installer. Now the error changes to "The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception."

I am using Windows 8 with VS2012 and Windows Azure Emulator version 2.0.0.

I will grateful if anyone can help me on this.

InnerException
    Message: The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception.

    Source: Microsoft.WindowsAzure.ServiceRuntime

    Stack Trace:    at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.get_IsAvailable()
       at Microsoft.ApplicationServer.Caching.AzureClientHelper.RoleUtility.IsAzureEnvironmentAvailable()

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.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Microsoft.ApplicationServer.Caching.DataCacheFactoryConfiguration.Initialize(String clientName)
   at Microsoft.ApplicationServer.Caching.DataCacheFactoryConfiguration..ctor()
   at Microsoft.ApplicationServer.Caching.DataCacheFactory..ctor()
   at Microsoft.ApplicationServer.Caching.DataCacheFactory.InitializeOrFetchSingletonFactoryInstance(String clientConfigurationName)
   at Microsoft.ApplicationServer.Caching.DataCache..ctor(String cacheName, String clientConfigurationName)
   at Microsoft.ApplicationServer.Caching.DataCache..ctor(String cacheName)
   at MvcWebRole.Controllers.HomeController.Index() in d:\Pankaj\Azure.Test\Caching.Sample\MvcWebRole\Controllers\HomeController.cs:line 15
   at lambda_method(Closure , ControllerBase , Object[] )
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
Bureaucrat answered 15/5, 2013 at 8:19 Comment(4)
Please post the full exception, including the InnerException.Dioptase
Exception: Exception has been thrown by the target of an invocation. Inner Exception: {"The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception."}Bureaucrat
There's really no more information than that? No stack trace?Dioptase
Please find the Exception details aboveBureaucrat
S
3

I experienced this when migrating from Azure SDK 2.3 to SDK 2.4.

I noticed the migration automatically fixed all references to the service runtime for my web and worker roles, i.e.

C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.3\ref\Microsoft.WindowsAzure.ServiceRuntime.dll

changed to:

C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.4\ref\Microsoft.WindowsAzure.ServiceRuntime.dll

However any assemblies referenced from the web/worker roles that referenced this assembly didn't update so I had to do it manually.

In addition I had to update the web.config and app.config entries to reference 2.4.0.0

<dependentAssembly>
    <assemblyIdentity name="Microsoft.WindowsAzure.ServiceRuntime" publicKeyToken="31bf3856ad364e35" culture="neutral" />
     <bindingRedirect oldVersion="0.0.0.0-2.4.0.0" newVersion="2.4.0.0" />
</dependentAssembly>
Stiletto answered 16/10, 2014 at 9:49 Comment(1)
I didnt migrated but got this issue with 2.3 verison in azure cloud. on local machine emulator it is working fine.Peanut
S
1

I had the same issue. The solution was to point all referenced Azure assemblies in every project to the same source (for me the dlls in the SDK folder). The NuGetManager copies the dlls into the main project path under packages and points the references to these dlls.

Spireme answered 7/6, 2013 at 14:57 Comment(0)
T
1

I've solved a similar issue by adding this to App.config:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

This is because, I believe, ServiceRuntime.dll (or one of its dependencies) is a "mixed-mode" assembly. A little more info about what that means, and the config lines above, can be found here: What does 'useLegacyV2RuntimeActivationPolicy' do in the .NET 4 config?

Tarnation answered 25/7, 2013 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.