AppFabric でキャッシュを使用する必要がある Azure ワーカー ロールがあります。
クラウド内のキャッシュをポイントしてローカル (Win7x64、VS2010) で実行すると、正常に動作します。
同じパッケージをクラウドにデプロイすると (再び同じキャッシュを指す)、次の例外が生成されます。
Message: The type initializer for 'Microsoft.ApplicationServer.Caching.DataCacheClientLogManager' threw an exception.
Exception Type: TypeInitializationException
StackTrace: Microsoft.ApplicationServer.Caching.DataCacheClientLogManager.Initialize(DataCacheLogSink logSink)
at Microsoft.ApplicationServer.Caching.DataCacheFactoryConfiguration.Initialize(String clientName)
at CommunicationRole.CacheUtil.GetCache()
コードを見ると、これは次のコード行がヒットしたときに発生しています。
Dim configuration As New DataCacheFactoryConfiguration()
この行にヒットした後は何も実行されません。前述したように、ローカルとクラウドの構成は同じです。Web 展開でセッション状態プロバイダーとして資格情報を含むキャッシュを使用しているため、キャッシュとキャッシュへのアクセスは良好であると思います。
私のビルド マシンには、2011 年 11 月リリースの Azure SDKがあり、Azure AppFabric SDK 1.5がインストールされています。
キャッシュを取得する方法は次のとおりです。
Imports System.IO
Imports Microsoft.WindowsAzure
Imports Microsoft.WindowsAzure.ServiceRuntime
Imports Microsoft.WindowsAzure.StorageClient
Imports Microsoft.ApplicationServer.Caching
Imports System.Security
Public Class CacheUtil
Private Shared _factory As DataCacheFactory = Nothing
Private Shared _cache As DataCache = Nothing
Public Shared Function GetCache() As DataCache
If _cache IsNot Nothing Then
Return _cache
End If
'-------------------------
' Configure Cache Client
'-------------------------
'Define Array for 1 Cache Host
Dim servers As New List(Of DataCacheServerEndpoint)()
'Specify Cache Host Details
' Parameter 1 = host name
' Parameter 2 = cache port number
servers.Add(New DataCacheServerEndpoint(RoleEnvironment.GetConfigurationSettingValue("hostName"), Int32.Parse(RoleEnvironment.GetConfigurationSettingValue("cachePort"))))
' Setup secure key
Dim strACSKey As String = RoleEnvironment.GetConfigurationSettingValue("authorisationToken")
Dim secureACSKey As New SecureString
For Each a As Char In strACSKey
secureACSKey.AppendChar(a)
Next
secureACSKey.MakeReadOnly()
Dim factorySecurity As New DataCacheSecurity(secureACSKey)
'Create cache configuration
Dim configuration As New DataCacheFactoryConfiguration()
configuration.Servers = servers
configuration.SecurityProperties = factorySecurity
'Disable tracing to avoid informational/verbose messages on the web page
DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off)
'Pass configuration settings to cacheFactory constructor
_factory = New DataCacheFactory(configuration)
'Get reference to named cache called "default"
_cache = _factory.GetCache(RoleEnvironment.GetConfigurationSettingValue("cacheName"))
Return _cache
End Function
Public Shared Sub Dispose()
If _factory IsNot Nothing Then
_factory.Dispose()
End If
End Sub
End Class