![](/img/trans.png)
[英]Getting error: Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an exception
[英]The type initializer for 'Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment' threw an 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'的類型初始化程序引發異常。”
我使用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)
我從Azure SDK 2.3遷移到SDK 2.4時遇到過這種情況。
我注意到遷移會自動修復對Web和worker角色的服務運行時的所有引用,即
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>
我遇到過同樣的問題。 解決方案是將每個項目中所有引用的Azure程序集指向同一個源(對我來說,SDK文件夾中的dll)。 NuGetManager將dll復制到packages
下的主項目路徑中,並指向對這些dll的引用。
我已經通過將此添加到App.config解決了類似的問題:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
這是因為,我相信,ServiceRuntime.dll(或其依賴項之一)是一個“混合模式”程序集。 關於這意味着什么以及上面的配置行的更多信息可以在這里找到: 'useLegacyV2RuntimeActivationPolicy'在.NET 4配置中做了什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.