来自response.Content.ReadAsStreamAsync()的流不是随机可读的

时间:2016-10-31 18:27:42

标签: c# asp.net-web-api async-await httpcontent httpresponsemessage

我正在创建一个.Net Web API应用程序,其中以下代码调用我的不同c#应用程序来下载文件,然后将其保存在磁盘上。有时一切正常,我得到了文件,但有时下面的代码无法读取流,我可以看到远程连接在我的其他应用程序中关闭异常。

public async Task<string> GetFilePathAsync(TestModel model)
{
    string filePath = string.Empty;
    var response = await cgRequestHelper.DownloadAsync(model);  

    if (response.IsSuccessStatusCode)
    {                    
        filePath = await SaveCgStreamAsync(cgResponse, serviceModel.FileName);
    }
    return filePath;
}

public async Task<HttpResponseMessage> DownloadAsync(TestModel model)
{            
    if (model == null)
        throw new ArgumentNullException("model");
    if (string.IsNullOrEmpty(model.Url))
        throw new ArgumentNullException("Url");
    if (model.Headers == null)
        throw new ArgumentNullException("Headers");

    HttpResponseMessage response;
    using (HttpClient httpClient = new HttpClient())
    {
        foreach (var header in model.Headers)
        {
            httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
        }
        response = await httpClient.GetAsync(model.Url, HttpCompletionOption.ResponseHeadersRead); 
    }
    return response;            
}        

public async Task<string> SaveCgStreamAsync(HttpResponseMessage response, string fileName)
{
    if (response == null)
        throw new ArgumentNullException("response");
    if (string.IsNullOrEmpty(fileName))
        throw new ArgumentNullException("fileName");

    var filePath = _CreateTemporaryLocation(fileName);
    Stream cgStream = null;
    Stream fileStream = null;
    try
    {
        cgStream =  await response.Content.ReadAsStreamAsync();
        fileStream = File.Open(filePath, FileMode.Create);
        await cgStream.CopyToAsync(fileStream);
    }
    catch(Exception e)
    {
        throw;
    }
    finally
    {
        if(cgStream != null)
            cgStream.Dispose();
        if(fileStream != null)
            fileStream.Dispose();
    }

    return filePath;            
}

我在Global.asax.cs中设置了ServicePointManager.DefaultConnectionLimit = 1000

在我当前的应用程序中,我在“await cgStream.CopyToAsync(fileStream);”上遇到异常。尝试读取cgStream时的行。唯一的例外是“无法访问已关闭的流”。使用null InnerException 另一个应用程序例外是: System.Web.HttpException:远程主机关闭了连接。错误代码是0x800703E3。    在System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result,Boolean throwOnDisconnect)    在System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()    在System.Web.HttpResponse.Flush(Boolean finalFlush,Boolean async)

平均而言,10个请求中有1个因上述错误而失败。由于它是随机的并且并非总是失败,因此很难解决问题。

上述代码是在http://www.tugberkugurlu.com/archive/efficiently-streaming-large-http-responses-with-httpclient

的帮助下编写的

感谢任何与此问题相关的帮助!

由于

3 个答案:

答案 0 :(得分:3)

我在代码中找出了问题: 问题是我在'使用'中初始化HttpClient对象并在使用范围之外使用它的响应。这将处置HttpClient对象,从而打破远程连接,这就是我无法流式传输内容的原因。随机行为是因为我们不知道对象什么时候会被处理掉,有时它在流式传输之前不会被处理掉,有时它会被处理掉。

答案 1 :(得分:0)

我会尝试Stream.CopyTo而只是为了让您知道我之前尝试过以下代码,后来将其更改为CopyToAsync

public async Task<string> GetFilePathAsync(TestModel model)
    {
        string filePath = string.Empty;
        var response = await cgRequestHelper.DownloadAsync(model);  

        if (response.IsSuccessStatusCode)
        {                   
            Stream cgStream = await response.Content.ReadAsStreamAsync();
            filePath = SaveCgStream(cgStream , model.FileName);
        }
        return filePath;
    }

    public string SaveCgStream(Stream cgStream, string fileName)
        {
            if (response == null)
                throw new ArgumentNullException("response");
            if (string.IsNullOrEmpty(fileName))
                throw new ArgumentNullException("fileName");

            const int CHUNK_SIZE = 40000;
            var filePath = _CreateTemporaryLocation(fileName);
            var fileStream = File.Create(filePath, CHUNK_SIZE);

            int bytesRead;
            var buffer = new byte[CHUNK_SIZE];
            bool isReadStarted = false;
            try
            {
                do
                {
                    bytesRead = cgStream.Read(buffer, 0, CHUNK_SIZE);
                    if (bytesRead > 0)
                    {
                        isReadStarted = true;
                        fileStream.Write(buffer, 0, bytesRead);
                    }
                } while (bytesRead > 0);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                fileStream.Close();
            }
            return filePath;
        }

我在bytesRead = cgStream.Read(buffer, 0, CHUNK_SIZE);行中遇到了同样的错误。你还认为这是CopyToAsync问题吗? 我对此表示怀疑,因为当我提出一个断点时,我可以看到该流是不可读的,因为Read属性在进入CopyToAsync方法之前是假的。

我尝试了CopyTo而不是CopyToAsync,但没有帮助。截图:

enter image description here

答案 2 :(得分:-1)

根据文档,Stream.CopyToAsync是异步的。这意味着它不会阻止调用者(您的方法),这将继续在finally块中处理流。换句话说,你有竞争条件。切换到Stream.CopyTo你应该很高兴。

相关问题