Share via


异常处理概述(Windows Server AppFabric 缓存)

发生错误时,Windows Server AppFabric 的缓存 API 引发 .NET 异常。本主题介绍基本错误处理概念并提供示例。

DataCacheException 类

为特定于 AppFabric 缓存方法的错误引发常见 DataCacheException 对象。DataCacheException 对象包含可以帮助您诊断异常原因的四个属性:

DataCacheException 属性 描述

Message

描述错误的字符串。

ErrorCode

DataCacheErrorCode 类中错误代码常量对应的整数值。

SubStatus

DataCacheErrorSubStatus 类中子状态常量对应的整数值。

InnerException

导致当前异常的异常实例。此值可以为 Null。

任何缓存客户端方法可能发生某些失败,例如超时。您的应用程序代码应准备用于处理这些常见异常。有关详细信息,请参阅常见异常(Windows Server AppFabric 缓存)

备注

某些失败不会引发异常。例如,如果未找到密钥,则 Get 方法返回 Null。其他方法可能返回布尔值以指示成功或失败。有关特定方法的详细信息,请参阅 Windows Server AppFabric 类库文档中的 Microsoft.ApplicationServer.Caching 命名空间。

示例

以下示例尝试将名为 strObject 的字符串对象放入名为myCacheDataCache 中。过载的 Put 方法用于指定对象的缓存区域。如果此区域尚不存在于缓存中,则将使用错误代码 RegionDoesNotExist 引发 DataCacheException 对象。在此示例中,通过创建区域和重试 put 操作处理此错误。

Dim strKey As String = "key0"
Dim strObject As String = "Source String"

Try
   ' Put a string object into the cache region, "Region1"
   myCache.Put(strKey, strObject, "Region1")

Catch cacheError As DataCacheException
   ' Look at the ErrorCode property to see if the Region is missing
   If (cacheError.ErrorCode = DataCacheErrorCode.RegionDoesNotExist) Then

      ' Create the Region and retry the Put call
      myCache.CreateRegion("Region1")
      myCache.Put(strKey, strObject, "Region1")
   End If
End Try
string strKey = "key0";
string strObject = "Source String";

try
{
   // Put a string object into the cache region, "Region1"
   myCache.Put(strKey, strObject, "Region1");
}
catch (DataCacheException cacheError)
{
   // Look at the ErrorCode property to see if the Region is missing
   if (cacheError.ErrorCode == DataCacheErrorCode.RegionDoesNotExist)
   {
      // Create the Region and retry the Put call
      myCache.CreateRegion("Region1");
      myCache.Put(strKey, strObject, "Region1");
   }
}

另请参阅

概念

常见异常(Windows Server AppFabric 缓存)
配置缓存客户端超时(Windows Server AppFabric 缓存)

  2011-12-05