【发布时间】:2020-08-24 13:02:11
【问题描述】:
不幸的是,我几乎陷入了以下情况:
我正在使用 .NET Core 3.1 并通过 DI 使用 HttpClient(类型化客户端)从 url 获取数据。此数据是一个 csv 文件,应上传到 Azure Blob 存储。在我尝试将其上传到 Blob 存储的地方,它似乎失败了,并出现以下异常:
System.Net.Http: The operation was canceled. System.Net.Http: Error while copying content to a stream.
System.Net.Sockets: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request..
The I/O operation has been aborted because of either a thread exit or an application request.
我目前不知道为什么上传不起作用。来自其他流的其他上传似乎没有任何错误。你们有谁知道我的线索吗?
我的课
public class Web<T> where T : BlobStorageContext
{
private HttpClient _client { get; }
private ILogger<Web> _logger;
private Csv<T> _csv { get; }
public Web(HttpClient client, Csv<T> csv, ILogger<Web> logger)
{
_client = client;
_logger = logger;
_csv = csv;
}
public async Task AccessWebFileAndSaveToAzureAsync()
{
...
var request = new HttpRequestMessage(HttpMethod.Get,
url);
using (var response = await _client.SendAsync(request))
{
await using (var str = await response.Content.ReadAsStreamAsync())
{
await _csv.WriteFileToAzureAsync(str);
}
}
}
}
public class Csv<T> where T : BlobStorageContext
{
private BlobServiceClient _blobServiceClient { get; set; }
private ILogger<Csv<T>> _logger { get; set; }
public Csv(IOptions<BlobStorageConfiguration<T>> configuration, ILogger<Csv<T>> logger)
{
_blobServiceClient = new BlobServiceClient(configuration.Value.ConnectionString());
_logger = logger;
}
public async Task WriteFileToAzureAsync(Stream str)
{
var container = _blobServiceClient.GetBlobContainerClient("container);
var blob = container.GetBlobClient(blobPath);
await blob.UploadAsync(str, true);
}
}
【问题讨论】:
-
我猜发生的情况是您上传到 Azure 时保持对应用程序的原始 HTTP 请求处于打开状态,并且在一定超时后,ASP.NET Core 会终止来自客户端的请求。您是否尝试过使用例如 Fiddler 来查看发生了什么?
-
您是否在 blob.UploadAsync 行上遇到异常?你马上就明白了吗?
-
尚未使用任何其他工具@CodeCaster 您认为将 responseStream 获取到我的 blob 存储需要另一种方法吗?我是否必须将其临时保存到另一个位置然后处理它?时间过去了,大约几分钟,直到发生此异常。马格努斯
标签: c# .net-core httpclient azure-blob-storage