Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

c# - The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception

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:PankajAzure.TestCaching.SampleMvcWebRoleControllersHomeController.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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 FilesMicrosoft SDKsWindows Azure.NET SDKv2.3
efMicrosoft.WindowsAzure.ServiceRuntime.dll

changed to:

C:Program FilesMicrosoft SDKsAzure.NET SDKv2.4
efMicrosoft.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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...