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

I am trying to use Windows Azure caching to store sessions in an MVC4 application. I am creating an application following the steps from Link , but when I try to create a DataCache object using the line of code below.

DataCache cache = new DataCache("default"); 

Errors occur:

Microsoft.WindowsAzure.ServiceRuntime.dll was not found or the version was not updated, I upgraded the version of Windows Azure Emulator to 2.0.0 and installed the WindowsAzure.Caching 2.0.0.0 package using the NuGet installer package. Now the error changes to "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 would be grateful if anyone could help me with 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) 
+6
source share
3 answers

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

I noticed that the migration automatically fixed all links to the runtime of the service for my web and work 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 that link to web / worker roles that link to this assembly were not updated, so I had to do this manually.

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

 <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> 
+2
source

I had the same problem. The solution was to point all the Azure reference assemblies in each project to the same source (for me, the DLL in the SDK folder). NuGetManager copies the DLL to the main project path in packages and points to the links to these DLLs.

+1
source

I solved a similar problem by adding this to App.config:

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

This is because, I suppose, ServiceRuntime.dll (or one of its dependencies) is a "mixed mode" assembly. A bit more information on what this means and the configuration lines above can be found here: What does 'useLegacyV2RuntimeActivationPolicy' do in .NET 4 configuration?

+1
source

Source: https://habr.com/ru/post/945050/


All Articles