小编use*_*152的帖子

Web API服务在读取流时挂起

说明:我正在修改支持可恢复文件上载的ASP.NET Core Web API服务(托管在Windows服务中).这很好,并在许多故障条件下恢复文件上传,除了下面描述的一个.

问题:当服务在其他计算机上并且客户端在我的计算机上并且我拔掉计算机上的电缆时,客户端会在服务挂起在fileSection.FileStream.Read()时检测到网络不存在.有时服务会在8分钟内检测到故障,有时在20分钟内检测到故障

我还注意到,在拔掉电缆并停止客户端之后,服务停留在Read()函数并且文件大小为x KB,但是当服务最后一段时间检测到异常时,它会向文件写入额外的4 KB .这很奇怪,因为我关闭了缓冲,缓冲区大小为2 KB.

问题:如何正确检测服务上缺少网络,或正确超时,或取消请求

服务代码:

public static async Task<List<(Guid, string)>> StreamFileAsync(
   this HttpRequest request, DeviceId deviceId, FileTransferInfo transferInfo)
    {
        var boundary = GetBoundary(MediaTypeHeaderValue.Parse(request.ContentType), DefaultFormOptions.MultipartBoundaryLengthLimit);
        var reader = new MultipartReader(boundary, request.Body);
        var section = await reader.ReadNextSectionAsync(_cancellationToken);

        if (section != null)
        {
            var fileSection = section.AsFileSection();
            var targetPath = transferInfo.FileTempPath;

            try
            {
                using (var outfile = new FileStream(transferInfo.FileTempPath, FileMode.Append, FileAccess.Write, FileShare.None))
                {
                    var buffer = new byte[DefaultCopyBufferSize];
                    int read;

                    while ((read …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net asp.net-web-api asp.net-core-webapi

6
推荐指数
1
解决办法
244
查看次数

标签 统计

.net ×1

asp.net ×1

asp.net-core-webapi ×1

asp.net-web-api ×1

c# ×1