用于 .NET 的媒体服务 SDK 中的重试逻辑Retry logic in the Media Services SDK for .NET

使用 Azure 服务时,可能会发生暂时性故障。When working with Azure services, transient faults can occur. 如果发生暂时性故障,在大多数情况下,重试几次后操作即可成功。If a transient fault occurs, in most cases, after a few retries the operation succeeds. 用于 .NET 的媒体服务 SDK 可实现重试逻辑,用于处理与异常和错误相关的暂时性故障,引起这些异常和错误的原因包括 Web 请求、执行查询、保存更改和存储操作。The Media Services SDK for .NET implements the retry logic to handle transient faults associated with exceptions and errors that are caused by web requests, executing queries, saving changes, and storage operations. 默认情况下,适用于 .NET 的媒体服务 SDK 向应用程序重新引发异常前会执行四次重试。By default, the Media Services SDK for .NET executes four retries before re-throwing the exception to your application. 应用程序中的代码稍后必须正确处理此异常。The code in your application must then handle this exception properly.

下面是 Web 请求、存储、查询和 SaveChanges 策略的简要原则:The following is a brief guideline of Web Request, Storage, Query, and SaveChanges policies:

  • 存储策略用于 Blob 存储操作(上传或下载资产文件)。The Storage policy is used for blob storage operations (uploads or download of asset files).

  • Web 请求策略用于泛型 Web 请求(例如,获取身份验证令牌和解析用户群集终结点)。The Web Request policy is used for generic web requests (for example, for getting an authentication token and resolving the users cluster endpoint).

  • 查询策略用于查询 REST 中的实体(例如,mediaContext.Assets.Where(...))。The Query policy is used for querying entities from REST (for example, mediaContext.Assets.Where(…)).

  • SaveChanges 策略用于执行服务中更改数据的任何操作(如创建实体、更新实体以及为操作调用服务函数)。The SaveChanges policy is used for doing anything that changes data within the service (for example, creating an entity updating an entity, calling a service function for an operation).

    本主题列出了用于 .NET 的媒体服务 SDK 的重试逻辑可处理的异常类型和错误代码。This topic lists exception types and error codes that are handled by the Media Services SDK for .NET retry logic.

异常类型Exception types

下表介绍了用于 .NET 的媒体服务 SDK 对于可能导致暂时性故障的操作可处理和不可处理的异常。The following table describes exceptions that the Media Services SDK for .NET handles or does not handle for some operations that may cause transient faults.

异常Exception Web 请求Web Request 存储Storage 查询Query SaveChangesSaveChanges
WebExceptionWebException
有关详细信息,请参阅 WebException 状态代码部分。For more information, see the WebException status codes section.
Yes Yes Yes Yes
DataServiceClientExceptionDataServiceClientException
有关详细信息,请参阅 HTTP 错误和状态代码For more information, see HTTP error status codes.
No Yes Yes Yes
DataServiceQueryExceptionDataServiceQueryException
有关详细信息,请参阅 HTTP 错误和状态代码For more information, see HTTP error status codes.
No Yes Yes Yes
DataServiceRequestExceptionDataServiceRequestException
有关详细信息,请参阅 HTTP 错误和状态代码For more information, see HTTP error status codes.
No Yes Yes Yes
DataServiceTransportExceptionDataServiceTransportException No No Yes Yes
TimeoutExceptionTimeoutException Yes Yes Yes No
SocketExceptionSocketException Yes Yes Yes Yes
StorageExceptionStorageException No Yes No No
IOExceptionIOException No Yes No No

WebException 状态代码WebException status codes

下表介绍了针对其实现重试逻辑的 WebException 错误代码。The following table shows for which WebException error codes the retry logic is implemented. WebExceptionStatus 枚举定义状态代码。The WebExceptionStatus enumeration defines the status codes.

状态Status Web 请求Web Request 存储Storage 查询Query SaveChangesSaveChanges
ConnectFailureConnectFailure Yes Yes Yes Yes
NameResolutionFailureNameResolutionFailure Yes Yes Yes Yes
ProxyNameResolutionFailureProxyNameResolutionFailure Yes Yes Yes Yes
SendFailureSendFailure Yes Yes Yes Yes
PipelineFailurePipelineFailure Yes Yes Yes No
ConnectionClosedConnectionClosed Yes Yes Yes No
KeepAliveFailureKeepAliveFailure Yes Yes Yes No
UnknownErrorUnknownError Yes Yes Yes No
ReceiveFailureReceiveFailure Yes Yes Yes No
RequestCanceledRequestCanceled Yes Yes Yes No
超时Timeout Yes Yes Yes No
ProtocolErrorProtocolError
ProtocolError 的重试受控于 HTTP 状态代码处理。The retry on ProtocolError is controlled by the HTTP status code handling. 有关详细信息,请参阅 HTTP 错误和状态代码For more information, see HTTP error status codes.
Yes Yes Yes Yes

HTTP 错误状态代码HTTP error status codes

如果 Query 和 SaveChanges 操作引发 DataServiceClientException、DataServiceQueryException 或 DataServiceQueryException,则 StatusCode 属性中返回 HTTP 错误状态代码。When Query and SaveChanges operations throw DataServiceClientException, DataServiceQueryException, or DataServiceQueryException, the HTTP error status code is returned in the StatusCode property. 下表介绍了为哪些错误代码实现了重试逻辑。The following table shows for which error codes the retry logic is implemented.

状态Status Web 请求Web Request 存储Storage 查询Query SaveChangesSaveChanges
401401 No Yes No No
403403 No Yes
处理等待时间较长的重试。Handling retries with longer waits.
No No
408408 Yes Yes Yes Yes
429429 Yes Yes Yes Yes
500500 Yes Yes Yes No
502502 Yes Yes Yes No
503503 Yes Yes Yes Yes
504504 Yes Yes Yes No

若要查看适用于 .NET 的媒体服务 SDK 重试逻辑的实际实现,请参阅 azure-sdk-for-media-servicesIf you want to take a look at the actual implementation of the Media Services SDK for .NET retry logic, see azure-sdk-for-media-services.

后续步骤Next steps

媒体服务 v3(最新版本)Media Services v3 (latest)

查看最新版本的 Azure 媒体服务!Check out the latest version of Azure Media Services!

媒体服务 v2(旧版)Media Services v2 (legacy)