我正在开发一个新的.NET核心服务的应用程序接口,新的应用程序接口应该是从SQL表读取BLOB,使用DeflateStream解压缩它。然后将其返回(流式传输)到客户端。
为了不消耗太多内存。我返回一个类型为PushStreamContent的响应,这样我就可以直接将SQL流复制到响应流中,而不需要在内存中加载blob。所以我最终得到了类似的东西。
return this.ResponseMessage(new HttpResponseMessage
{
Content = new PushStreamContent(async (outStream, httpContent, transportContext) =>
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
await connection.OpenAsync();
using (SqlCommand command = new SqlCommand(query, connection))
{
// The reader needs to be executed with the SequentialAccess behavior to enable network streaming
// Otherwise ReadAsync will buffer the entire BLOB into memory which can cause scalability issues or even OutOfMemoryExceptions
using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
{
if (await reader.ReadAsync() && !(await reader.IsDBNullAsync(0)))
{
using (Stream streamToDecompress = reader.GetStream(0))
using (Stream decompressionStream = new DeflateStream(streamToDecompress, CompressionMode.Decompress))
{
// This copyToAsync will take for ever
await decompressionStream.CopyToAsync(outStream);
outStream.close();
return;
}
}
throw new Exception("Couldn't retrieve blob");
}
}
}
},
"application/octet-stream")
});这里的问题是,如代码中所述,将deflateStream复制到响应输出流所需的步骤永远不会发生。虽然我尝试了相同的方法,但将流写入文件而不是将其复制到resp流,它的效果很好。
你们能帮我吗?关于使用PushStreamContent,我说错了吗?我应该使用不同的方法吗?问题是,我不想在内存中加载整个Blob,我想读取它并在运行时将其解压缩。SqlClient Supports streaming blobs和我想利用这一点。
发布于 2020-07-21 05:08:02
这是PushStreamContent中的死锁,我不想假装理解它。但我重现了它并改变了它
await decompressionStream.CopyToAsync(outStream);至
decompressionStream.CopyTo(outStream);解决了这个问题。
下面是完整的复制品:
public ResponseMessageResult Get()
{
var data = new string[] { "value1", "value2" };
var jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(data);
var msSource = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));
var msDest = new MemoryStream();
var compressionStream = new DeflateStream(msDest, CompressionMode.Compress);
msSource.CopyTo(compressionStream);
compressionStream.Close();
var compressedBytes = msDest.ToArray();
var query = "select @bytes buf";
var connectionString = "server=localhost;database=tempdb;integrated security=true";
return this.ResponseMessage(new HttpResponseMessage
{
Content = new PushStreamContent(async (outStream, httpContent, transportContext) =>
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
await connection.OpenAsync();
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.Add("@bytes", SqlDbType.VarBinary, -1).Value = compressedBytes;
// The reader needs to be executed with the SequentialAccess behavior to enable network streaming
// Otherwise ReadAsync will buffer the entire BLOB into memory which can cause scalability issues or even OutOfMemoryExceptions
using (SqlDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess))
{
if (await reader.ReadAsync() && !(await reader.IsDBNullAsync(0)))
{
using (Stream streamToDecompress = reader.GetStream(0))
{
//var buf = new MemoryStream();
//streamToDecompress.CopyTo(buf);
//buf.Position = 0;
using (Stream decompressionStream = new DeflateStream(streamToDecompress, CompressionMode.Decompress))
{
// This copyToAsync will take for ever
//await decompressionStream.CopyToAsync(outStream);
decompressionStream.CopyTo(outStream);
outStream.Close();
return;
}
}
}
throw new Exception("Couldn't retrieve blob");
}
}
}
},
"application/octet-stream")
});
}https://stackoverflow.com/questions/62985060
复制相似问题