简体   繁体   English

带有ExecuteReaderAsync的ASP.NET webapi HttpResponseMessage CommandBehavior.SequentialAccess

[英]ASP.NET webapi HttpResponseMessage with ExecuteReaderAsync CommandBehavior.SequentialAccess

I need to stream blob data from Sql Server via WebApi. 我需要通过WebApi从Sql Server传输blob数据。

I DONT want to buffer the blob data in memory on the web server. 我不想在Web服务器上缓冲内存中的blob数据。

I have the following code, but it does not work - there is NO exception. 我有以下代码,但它不起作用 - 没有例外。

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 writes the following error as the reposnse: [Fiddler] ReadResponse() failed: The server did not return a response for this request. Fiddle将以下错误写为reposnse:[Fiddler] ReadResponse()失败:服务器未返回此请求的响应。

How can I stream the content from the DB to the http output stream, with out buffering it in memory? 如何将内容从数据库流式传输到http输出流,而不在内存中缓冲它?

You are closing the stream before ASP.NET MVC has finished reading from it. 在ASP.NET MVC完成读取之前,您正在关闭流。 It will be closed once you leave the various using blocks which happens right after the return statement has executed. 一旦你离开各种使用块,它将在return语句执行后立即关闭。

I know of no easy way to accomplish this. 我知道没有简单的方法来实现这一目标。 The best idea would be to write a custom Stream -derived class that wraps the stream returned by ADO.NET and once the stream is depleted disposes of everything (the Stream , the reader, the command and the connection). 最好的想法是编写一个自定义的Stream -derived类,它包装ADO.NET返回的流,一旦流耗尽,就会处理所有内容( Stream ,reader,command和connection)。

This solution would mean that you cannot use using blocks and such. 此解决方案意味着您不能使用块等。 I really don't like it but I cannot think of something better at the moment. 我真的不喜欢它,但我现在想不出更好的东西。 The requirements are difficult in combination: you want streaming behavior and need to dispose of the various resources you opened. 这些要求很难组合:您需要流式传输行为并需要处理您打开的各种资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM