gpt4 book ai didi

c# - 存储获取 blob TaskCanceledException

转载 作者:行者123 更新时间:2023-12-03 04:06:10 26 4
gpt4 key购买 nike

我的应用程序使用 Azure 存储 blob 将文件从远程存储下载到本地计算机。

工作原理:

  1. 计算机获取带有 Azure 存储 blob 的 URL(签名 URL)的队列消息。
  2. 应用程序发送 GET 来下载文件。这是通过 HttpClient 完成的。
  3. 应用逻辑从这里继续。

这是我下载文件的方式:

HttpRequestMessage msg = new HttpRequestMessage(HttpMethod.Get, signedUrl);
HttpClient.Timeout = TimeSpan.FromSeconds(120);
var response = await HttpClient.SendAsync(msg);
response.EnsureSuccessStatusCode();

我发现每天都会有几次出现异常:

System.Threading.Tasks.TaskCanceledException: The operation was canceled. ---> System.IO.IOException: Unable to read data from the transport connection: Operation canceled. ---> System.Net.Sockets.SocketException: Operation canceled --- End of inner exception stack trace --- at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error) at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token) at System.Net.Security.SslStreamInternal.g__InternalFillBufferAsync|38_0[TReadAdapter](TReadAdapter adap, ValueTask1 task, Int32 min, Int32 initial)
at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory
1 buffer) at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Polly.Retry.AsyncRetryEngine.ImplementationAsync[TResult](Func
3 action, Context context, CancellationToken cancellationToken, ExceptionPredicates shouldRetryExceptionPredicates, ResultPredicates1 shouldRetryResultPredicates, Func5 onRetryAsync, Int32 permittedRetryCount, IEnumerable1 sleepDurationsEnumerable, Func4 sleepDurationProvider, Boolean continueOnCapturedContext) at Polly.AsyncPolicy1.ExecuteAsync(Func3 action, Context context, CancellationToken cancellationToken, Boolean continueOnCapturedContext)

此时,我必须澄清这种行为发生在系统中传递的约 0.1% 的 URL 中。

我怀疑问题出在签名 URL 上。我发现在所有情况下签名的 URL 都是正确的 - 我只需将其放入浏览器中即可正常工作。

现在,当我知道问题不是 URL 本身时,我认为可能是网络问题。因此,通过使用 Polly我添加了 5 次重试,每次重试之间都有指数延迟。最多重试 5 次。延迟公式 - 3^n(其中 n 是重试次数)。在最坏的情况下,它会等待 4 分钟再重试...即使现在 - 仍然是同样的错误。 5 次重试均已使用,但文件未下载。

该怎么办?我还应该检查什么?

最佳答案

似乎是网络问题。我建议您使用Azure存储客户端SDK下载blob。

// Blob SAS URL
string url = "https://storagetest789.blob.core.windows.net/devops/deploy.ps1?sp=r&st=2020-01-21T09:39:06Z&se=2020-01-21T17:39:06Z&spr=https&sv=2019-02-02&sr=b&sig=tyGX6wpgAooOZK2Po2ntukF0fGx3PosSNTgkLlVysrE%3D";
CloudBlockBlob blob = new CloudBlockBlob(new Uri(url));
Console.WriteLine(blob.DownloadText());

您还可以使用 SDK 方法下载到文件或流式传输。 SDK可以处理所有底层的东西。

关于c# - 存储获取 blob TaskCanceledException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59836258/

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