0

.NET Core サービスで新しい API を開発しています。新しい API は、SQL テーブルから BLOB を読み取り、DeflateStream を使用して解凍することになっています。そして、それをクライアントに返します (ストリーミングします)。

多くのメモリを消費しないために。タイプとPushStreamContentの応答を返すので、BLOB をメモリにロードせずに SQL ストリームを直接応答ストリームにコピーできます。だから私はそのようなものになりました。

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全体をメモリにロードしたくないということです.Blobをその場で読み込んで解凍したいのです. SqlClient はストリーミング BLOB をサポートしており、それを利用したいと考えています。

4

1 に答える 1