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

C# 带有ExecuteReaderAsync CommandBehavior.SequentialAccess的ASP.NET webapi HttpResponseMessage,c#,asp.net,sql-server,asp.net-web-api,async-await,C#,Asp.net,Sql Server,Asp.net Web Api,Async Await,我需要通过WebApi从Sql Server传输blob数据 我不想在web服务器的内存中缓冲blob数据 我有以下代码,但它不工作-没有例外 public class AttachmentController : ApiController { public async Task<HttpResponseMessage> Get(int id) { using (var connection = new SqlConnection(Configura

我需要通过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);
        }
    }
}
公共类AttachmentController:ApicController
{
公共异步任务Get(int-id)
{
使用(var connection=new SqlConnection(ConfigurationManager.ConnectionString[“ConnectionString”].ConnectionString))
{
等待连接。OpenAsync();
使用(var command=newsqlcommand(“从[Attachments]中选择内容,其中ID=@ID”,connection))
{
command.Parameters.AddWithValue(“ID”,ID);
使用(SqlDataReader=await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
{
if(等待reader.ReadAsync())
{
使用(流数据=reader.GetStream(0))
{
var response=newhttpresponsemessage{Content=newstreamcontent(data)};
//我从DB那里得到这个,还有别的地方吗
//response.Content.Headers.ContentType=新的MediaTypeHeaderValue(附件.ContentType);
//我从DB那里得到这个,还有别的地方吗
//response.Content.Headers.ContentLength=附件.ContentLength;
返回响应;
}
}
}
}
抛出新的HttpResponseException(HttpStatusCode.NotFound);
}
}
}
Fiddle将以下错误写入Resose: [Fiddler]ReadResponse()失败:服务器未对此请求返回响应


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

在ASP.NET MVC完成读取之前,您正在关闭该流。在执行
return
语句之后,一旦您离开各种using块,它就会关闭

我知道要做到这一点不容易。最好的办法是编写一个自定义的
派生类,该类封装ADO.NET返回的流,一旦流耗尽,就会处理所有内容(流、读取器、命令和连接)


此解决方案意味着您不能使用块等。我真的不喜欢,但我现在想不出比这更好的了。这些要求结合起来很困难:您需要流行为,并且需要处理您打开的各种资源。

我也在考虑类似的事情:创建一个从
StreamContent
派生的自定义类,该类重写
dispose()
方法。