【问题标题】:ASP.NET webapi HttpResponseMessage with ExecuteReaderAsync CommandBehavior.SequentialAccess带有 ExecuteReaderAsync CommandBehavior.SequentialAccess 的 ASP.NET webapi HttpResponseMessage
【发布时间】:2013-08-19 18:11:30
【问题描述】:

我需要通过 WebApi 从 Sql Server 流式传输 blob 数据。

我不想在 web 服务器的内存中缓冲 blob 数据。

我有以下代码,但它不起作用 - 没有异常。

public class AttachmentController : ApiController
{
    public async Task<HttpResponseMessage> Get(int id)
    {
        using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            await connection.OpenAsync();

            using (var command = new SqlCommand("SELECT Content FROM [Attachments] WHERE ID = @ID", connection))
            {

                command.Parameters.AddWithValue("ID", id);

                using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
                {

                    if (await reader.ReadAsync())
                    {
                        using (Stream data = reader.GetStream(0))
                        {
                            var response = new HttpResponseMessage{Content = new StreamContent(data)};
                            //I get this from the DB else where
                            //response.Content.Headers.ContentType = new MediaTypeHeaderValue(attachment.ContentType);
                            //I get this from the DB else where
                            //response.Content.Headers.ContentLength = attachment.ContentLength;
                            return response;

                        }
                    }
                }
            }

            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }
}

Fiddle 将以下错误写为响应: [Fiddler] ReadResponse() 失败:服务器没有返回对此请求的响应。

如何将内容从 DB 流式传输到 http 输出流,而不在内存中缓冲?

【问题讨论】:

    标签: c# asp.net sql-server asp.net-web-api async-await


    【解决方案1】:

    在 ASP.NET MVC 完成读取之前,您正在关闭流。一旦您离开在return 语句执行后立即发生的各种使用块,它将关闭。

    我知道没有简单的方法可以做到这一点。最好的办法是编写一个自定义的 Stream 派生类,该类包装 ADO.NET 返回的流,一旦流耗尽,就会处理所有内容(Stream、阅读器、命令和连接)。

    此解决方案意味着您不能使用 using 块等。我真的不喜欢它,但我现在想不出更好的东西。需求很难组合:你想要流式行为,需要处理你打开的各种资源。

    【讨论】:

    • 我在想类似的事情:创建一个从 StreamContent 派生的自定义类,它会覆盖 Dispose() 方法。
    猜你喜欢
    • 2017-06-18
    • 1970-01-01
    • 2017-08-05
    • 2012-08-22
    • 2014-09-09
    • 2016-05-20
    • 1970-01-01
    • 2023-03-29
    • 2023-04-10
    相关资源
    最近更新 更多