gpt4 book ai didi

c# - 取消读取文件信息的 OneDrive 任​​务时周期性超时异常

转载 作者:太空宇宙 更新时间:2023-11-03 12:19:55 25 4
gpt4 key购买 nike

我使用 Microsoft Graph C# SDK 来管理 OneDrive 文件。我的代码以并行方式处理存储(最大线程数 < 6)。为了取消不再有效的操作(用户关闭窗口或浏览到另一个文件夹),我使用了 CancellationToken

我有时会在取消任务时收到代码为 Error.Code = GraphErrorCode.TimeoutMicrosoft.Graph.ServiceException。这应该是 OperationCanceledException

如何解决?

代码很简单,所以我认为这是 Microsoft Graph 中的错误。 There are a lot of similar questions on StackOverflow .

SDK版本为1.7.0(1.5.0同样存在bug)

更新:

  • 将此错误与 StackOverflow 中的其他类似问题相关联的假设是无效的。
  • 我在 MSGraph github 上创建了错误.

最佳答案

我想我在 Microsoft Graph C# SDK 中发现了这个错误.

请看implementation of HttpProvider.SendRequestAsync method .
它包含以下代码:

    internal async Task<HttpResponseMessage> SendRequestAsync(
HttpRequestMessage request,
HttpCompletionOption completionOption,
CancellationToken cancellationToken)
{
try
{
return await this.httpClient.SendAsync(request, completionOption, cancellationToken).ConfigureAwait(false);
}
catch (TaskCanceledException exception)
{
throw new ServiceException(
new Error
{
Code = ErrorConstants.Codes.Timeout,
Message = ErrorConstants.Messages.RequestTimedOut,
},
exception);
}
catch (Exception exception)
{
throw new ServiceException(
new Error
{
Code = ErrorConstants.Codes.GeneralException,
Message = ErrorConstants.Messages.UnexpectedExceptionOnSend,
},
exception);
}
}

如您所见,它使用 Code = ErrorConstants.Codes.TimeoutTaskCanceledException 转换为 ServiceExceptionTaskCanceledException 是 .NET 4.5 在取消任务时抛出的异常(CancellationToken 标记为取消任务)。该异常类继承自OperationCanceledException

我只有一个问题要问 MS - 为什么!???
在MSDN的很多地方都有关于OperationCanceledException的短语是重要的异常,.NET任务管理代码使用它来验证取消原因并将任务切换到取消状态。任务的工作方法一定不能阻塞这个异常!如果您在任务代码中分析此异常,则必须重新抛出它以传播出去。以其他方式任务将以失败状态完成。只有运行任务并等待其完成的代码才能捕获此异常并对其进行分析。请阅读 - "Parallel Tasks", section "Canceling a Task"

If the cancellation token indicates that a cancellation has been requested, the ThrowIfCancellationRequested method creates an OperationCanceledException instance and passes in the cancellation token. It then throws the exception. This exception is the signal that notifies the .NET Framework that the task has been canceled; therefore, the OperationCanceledException should not be handled by user code within the task (it is often handled, however, outside of the task that was canceled). If you follow the steps that have been described in this section, the task will be stopped and its Status property will be set to the enumerated value TaskStatus.Canceled.

请删除catch(TaskCanceledException 异常)

更新:在我的代码中解决此错误的解决方法:

    // This method creates Task (because it's asynchronous) that can be canceled by token
public async Task<DriveItem> MyUsefulMethodAsync(string driveId, string itemId, string selectItemParameter, CancellationToken? cancellationToken = null)
{
DriveItem res = default(DriveItem);

try
{
// My useful code, for example:
res = await Client.Drives[driveId].Items[itemId].Request()
.Select(selectItemParameter)
.GetAsync(cancellationToken ?? CancellationToken.None);
}
catch (OperationCanceledException)
{
// We have to propagate out this exception for fine Task state managing.
throw;
}
catch (Exception e)
{
// This is workaround for MSGraph bug (https://stackoverflow.com/a/47900445/987850)
var se = e as ServiceException;
if (se?.InnerException is OperationCanceledException)
{
throw se.InnerException;
}

// My error processing ...
}

return res;
}

使用示例:

    try
{
// cancellation token from token source (can be invoked to cancel state by UI handlers)
var cancellationToken = m_bkCancelTokenSource.Token;

var driveItem = await MyUsefulMethodAsync(driveId, itemId, "id, name, parentReference, file, remoteItem, size", cancellationToken);
if (driveItem!=null)
{
// some code ...
}
}
catch(OperationCanceledException)
{
// task was canceled by user
}

关于c# - 取消读取文件信息的 OneDrive 任​​务时周期性超时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47890382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com