‘Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment’的类型初始值设定项引发exception

我正在尝试使用Windows Azure缓存在MVC4应用程序中存储会话。 我按照Link的步骤构建了一个应用程序,但是当我尝试使用下面的代码行创建DataCache的对象时。

DataCache cache = new DataCache("default"); 

发生错误:

找不到Microsoft.WindowsAzure.ServiceRuntime.dll或版本不匹配,我将Windows Azure Emulator版本更新为2.0.0并使用NuGet软件包安装程序安装WindowsAzure.Caching软件包版本2.0.0.0。 现在错误更改为“Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment的类型初始化程序”引发exception。

我使用Windows 8 with VS2012 and Windows Azure Emulator version 2.0.0.

如果有人能帮助我,我将不胜感激。

 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.b__12() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) 

从Azure SDK 2.3迁移到SDK 2.4时遇到过这种情况。

我注意到迁移会自动修复对我的Web和辅助角色的服务运行时的所有引用,即

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

变成:

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

但是,引用此程序集的Web /辅助角色引用的任何程序集都没有更新,因此我必须手动执行此操作。

另外,我必须更新web.config和app.config条目以引用2.4.0.0

     

我遇到过同样的问题。 解决方案是将每个项目中所有引用的Azure程序集指向同一个源(对我来说,SDK文件夹中的dll)。 NuGetManager将dll复制到packages下的主项目路径中,并指向对这些dll的引用。

我已经通过将此添加到App.config解决了类似的问题:

    

这是因为,我相信,ServiceRuntime.dll(或其依赖项之一)是一个“混合模式”程序集。 关于这意味着什么以及上面的配置行的更多信息可以在这里找到: ‘useLegacyV2RuntimeActivationPolicy’在.NET 4配置中做了什么?