Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebAPI StreamContent与PushStreamContent_C#_Visual Studio_Asp.net Mvc 4_Asp.net Web Api - Fatal编程技术网

C# WebAPI StreamContent与PushStreamContent

C# WebAPI StreamContent与PushStreamContent,c#,visual-studio,asp.net-mvc-4,asp.net-web-api,C#,Visual Studio,Asp.net Mvc 4,Asp.net Web Api,我正在实现一个all,它在我最初的尝试中运行得很好,但我正在努力确保在下载非常大的文件(~2GB)时,内存得到最佳利用 我已经尽我所能阅读并实现了它(删除异步部分-也许这就是问题所在?)。当我运行测试和观看TaskManager时,我看不到内存使用方面有多大差异,我试图理解响应处理方式之间的差异 以下是我的StreamContent版本: private HttpResponseMessage DownloadContentNonChunked() { var filename = Ht

我正在实现一个all,它在我最初的尝试中运行得很好,但我正在努力确保在下载非常大的文件(~2GB)时,内存得到最佳利用

我已经尽我所能阅读并实现了它(删除异步部分-也许这就是问题所在?)。当我运行测试和观看TaskManager时,我看不到内存使用方面有多大差异,我试图理解响应处理方式之间的差异

以下是我的StreamContent版本:

private HttpResponseMessage DownloadContentNonChunked()
{
    var filename = HttpContext.Current.Request["f"];
    var filePath = _storageRoot + filename;
    if (File.Exists(filePath))
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read));
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = filename
        };
        return response;
    }
    return ControllerContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, "");
}
public class FileDownloadStream
{
    private readonly string _filename;

    public FileDownloadStream(string filePath)
    {
        _filename = filePath;
    }

    public void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
    {
        try
        {
            var buffer = new byte[4096];

            using (var video = File.Open(_filename, FileMode.Open, FileAccess.Read))
            {
                var length = (int)video.Length;
                var bytesRead = 1;

                while (length > 0 && bytesRead > 0)
                {
                    bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
                    outputStream.Write(buffer, 0, bytesRead);
                    length -= bytesRead;
                }
            }
        }
        catch (HttpException ex)
        {
            return;
        }
        finally
        {
            outputStream.Close();
        }
    }
}

private HttpResponseMessage DownloadContentChunked()
{
    var filename = HttpContext.Current.Request["f"];
    var filePath = _storageRoot + filename;
    if (File.Exists(filePath))
    {
        var fileDownload = new FileDownloadStream(filePath);
        var response = Request.CreateResponse();
        response.Content = new PushStreamContent(fileDownload.WriteToStream, new MediaTypeHeaderValue("application/octet-stream"));
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = filename
        };
        return response;
    }
    return ControllerContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, "");
}
以下是我的PushStreamContent版本:

private HttpResponseMessage DownloadContentNonChunked()
{
    var filename = HttpContext.Current.Request["f"];
    var filePath = _storageRoot + filename;
    if (File.Exists(filePath))
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read));
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = filename
        };
        return response;
    }
    return ControllerContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, "");
}
public class FileDownloadStream
{
    private readonly string _filename;

    public FileDownloadStream(string filePath)
    {
        _filename = filePath;
    }

    public void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
    {
        try
        {
            var buffer = new byte[4096];

            using (var video = File.Open(_filename, FileMode.Open, FileAccess.Read))
            {
                var length = (int)video.Length;
                var bytesRead = 1;

                while (length > 0 && bytesRead > 0)
                {
                    bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
                    outputStream.Write(buffer, 0, bytesRead);
                    length -= bytesRead;
                }
            }
        }
        catch (HttpException ex)
        {
            return;
        }
        finally
        {
            outputStream.Close();
        }
    }
}

private HttpResponseMessage DownloadContentChunked()
{
    var filename = HttpContext.Current.Request["f"];
    var filePath = _storageRoot + filename;
    if (File.Exists(filePath))
    {
        var fileDownload = new FileDownloadStream(filePath);
        var response = Request.CreateResponse();
        response.Content = new PushStreamContent(fileDownload.WriteToStream, new MediaTypeHeaderValue("application/octet-stream"));
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = filename
        };
        return response;
    }
    return ControllerContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, "");
}
我的问题是,为什么我看不到这两种方法在内存使用方面有多大差异?此外,我下载了StreamContent类型的PDB,可以看到缓冲区大小等的引用(见下文),因此我想知道PushStreamContent在StreamContent之上和之外到底做了什么。我查看了MSDN上的类型信息,但文章的解释有点浅显

namespace System.Net.Http
{
  /// <summary>
  /// Provides HTTP content based on a stream.
  /// </summary>
  [__DynamicallyInvokable]
  public class StreamContent : HttpContent
  {
    private Stream content;
    private int bufferSize;
    private bool contentConsumed;
    private long start;
    private const int defaultBufferSize = 4096;

    /// <summary>
    /// Creates a new instance of the <see cref="T:System.Net.Http.StreamContent"/> class.
    /// </summary>
    /// <param name="content">The content used to initialize the <see cref="T:System.Net.Http.StreamContent"/>.</param>
    [__DynamicallyInvokable]
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public StreamContent(Stream content)
      : this(content, 4096)
    {
    }
namespace System.Net.Http
{
/// 
///基于流提供HTTP内容。
/// 
[[uuuu动态调用可禁用]
公共类StreamContent:HttpContent
{
私有流内容;
私有int缓冲区大小;
私人住宅;
私人长期启动;
private const int defaultBufferSize=4096;
/// 
///创建类的新实例。
/// 
///用于初始化的内容。
[[uuuu动态调用可禁用]
[TargetedPatchingOptOut(“性能对于跨NGen映像边界内联这种类型的方法至关重要”)]
公共流内容(流内容)
:此(内容,4096)
{
}

关于这两种方法的内存使用情况,对于StreamContent和PushStreamContent,Web API不会缓冲响应。下面的代码快照来自WebHostBufferPolicySelector。源代码


我认为这些都是相似的方法只是不同的分布。它们看起来确实相似-这就是为什么我问PushStreamContent如此详细,他们做了类似的事情,我想知道我应该使用哪一个作为thsi场景中的最佳实践!从我的测试中,我看到StreamContent正在加载先存储内存,然后刷新它。我建议不要将其用于大文件。您的文件下载流很好。我想知道
StreamContent
是否改变了行为。我认为旧版本的WebAPI会完全缓冲它,迫使人们使用
PushStreamContent
来解决这个问题,而现代版本,或者,当托管在OWIN也不缓冲,使两者相等。我想我会遵循-在什么情况下,你会使用PushStreamContent?因此,如果我想使用“流视频”这个词的意义上的“流”内容,请使用PushStreamContent,但如果我想让某人“下载文件”,则使用StreamContent?还有其他考虑吗?是的“流媒体视频"场景是这样一种情况,您事先不知道总内容长度,当您从某处接收视频源时,您正在写入目标流。这就是使用PushStreamContent响应以分块传输编码发送的原因,因为我们事先不知道内容长度。对于其他使用Owin和wonderin的用户g为什么他们的响应被缓冲不管他们做什么,5.0.0中有一个与缓冲相关的错误在5.1.0-RC1中得到了修复。我想和大家分享一下,如果我在五个小时前知道的话,我可能还有一些头发。更新包microsoft.aspnet.webapi.owin-includeprerelease是你的朋友。@bUKaneer:cases会使用PushStreamContent inc排除所有写入流的函数/对象。大多数序列化程序写入流。许多人写入内存流(这并不可怕,因为大多数序列化程序都不是异步的,因此会阻塞线程)。另一种情况可能是GZip流,尽管我记不起接口-它可能需要写入目标流。“流视频”是一个应用程序级答案。我希望我的答案更有用。此外,流视频可能是一个错误的答案-应该使用UDP,而不是HTTP/TCP。