首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解压缩SQL内容并在PushStreamContent .NET核心中返回答案

问解压缩SQL内容并在PushStreamContent .NET核心中返回答案
EN

Stack Overflow用户
提问于 2020-07-20 04:01:47
回答 1查看 78关注 0票数 0

我正在开发一个新的.NET核心服务的应用程序接口,新的应用程序接口应该是从SQL表读取BLOB,使用DeflateStream解压缩它。然后将其返回(流式传输)到客户端。

为了不消耗太多内存。我返回一个类型为PushStreamContent的响应,这样我就可以直接将SQL流复制到响应流中,而不需要在内存中加载blob。所以我最终得到了类似的东西。

代码语言:javascript
复制
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和我想利用这一点。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-21 05:08:02

这是PushStreamContent中的死锁,我不想假装理解它。但我重现了它并改变了它

代码语言:javascript
复制
await decompressionStream.CopyToAsync(outStream);

至

代码语言:javascript
复制
decompressionStream.CopyTo(outStream);

解决了这个问题。

下面是完整的复制品:

代码语言:javascript
复制
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")
    });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62985060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档