-->

The type initializer for 'Microsoft.WindowsAzu

2019-09-03 06:55发布

我想使用Windows Azure的缓存来存储会话在MVC4应用。 我建立以下从步骤的应用程序的链接 ,但是当我试图让使用下面的代码行数据高速缓存的对象。

DataCache cache = new DataCache("default");

错误发生:

Microsoft.WindowsAzure.ServiceRuntime.dll找不到或版本不匹配,我在我的Windows Azure模拟器版本更新至2.0.0,并使用NuGet包安装程序安装包WindowsAzure.Caching 2.0.0.0版本。 现在的错误更改为“的类型初始为‘Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment’引发了异常。”

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

Answer 1:

从Azure的SDK 2.3迁移到SDK 2.4,当我经历了这一点。

我注意到迁移自动固定在服务运行时的所有引用了我的网络和辅助角色,即

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

<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>


Answer 2:

我遇到过同样的问题。 解决的办法是指向每一个项目中的所有引用的Azure的组件,以相同的源(我在SDK文件夹中的DLL文件)。 该NuGetManager复制的dll进入主体工程路径下packages ,并指出这些DLL的引用。



Answer 3:

我加入这App.config中解决了类似的问题:

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

这是因为,我相信,ServiceRuntime.dll(或它的一个依赖)是一种“混合模式”组件。 关于这意味着什么,上面的配置线,可以在这里发现了一个小的详细信息: 什么是“useLegacyV2RuntimeActivationPolicy”在.NET 4的配置呢?



文章来源: The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception