WebApi를 통해 SQL Server에서 BLOB 데이터를 스트리밍해야합니다.ASP.NET webapi HttpResponseMessage with ExecuteReaderAsync CommandBehavior.SequentialAccess
I DONT는 웹 서버의 메모리에 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);
}
}
}
이 바이올린은 reposnse으로 다음과 같은 오류를 기록합니다 : 는 [피들러] ReadResponse() 실패 : 서버가이 요청에 대한 응답을 반환하지 않았습니다.
메모리에서 버퍼링하지 않고 DB의 내용을 http 출력 스트림으로 스트리밍 할 수 있습니까?
비슷한 생각 :'Dispose()'메소드를 오버라이드하는'StreamContent'에서 파생 된 맞춤 클래스를 생성하십시오. – svick