c# - 离开 StreamReader 而不关闭 Stream

标签 c# azure streamreader azure-servicebusrelay

我目前正在使用 Azure 中继服务,并在处理流时遇到问题。我需要以同步方式使用此服务,因为将使用此实现的软件需要它。

我正在打开服务的流并使用 StreamReader() 从中读取数据,工作正常。但现在我必须离开 StreamReader 而不关闭底层流,因为我必须将答案发送回发送者。

问题是,我无法在不关闭底层流的情况下离开 StreamReader() ,并且无法重新打开流以发回答案。

有什么想法可以解决这个问题吗?

感谢您的帮助。

最佳答案

有一个overload of the StreamReader constructor它接受一个 bool LeaveOpen 参数。传递 true 可防止 StreamReader 在释放 StreamReader 时关闭流。

leaveOpen

Type: System.Boolean

true to leave the stream open after the StreamReader object is disposed; otherwise, false.

示例,使用通过更简单的 StreamReader 构造函数获得的默认 UTF8 编码和 1024 字节缓冲区:

using (var reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true))
{
    // use reader
} // stream will NOT be closed here

关于c# - 离开 StreamReader 而不关闭 Stream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48564634/

相关文章:

c# - 多层 WinForm App 中的错误处理

c# - 如何更新 DrawingImage 中的颜色

azure - 如何将 Azure blob 文件下载到下载文件夹中,而不是在浏览器中

c# - 内存不足异常 - 读取 HttpWebRequest 的大文本文件

c# - 在 C# 中读取和写入非常大的文本文件

c# - C# 属性组

azure - 手动触发Azure Function - 时间触发

c# - 将 Azure Function 从门户导入 Visual Studio

c# - 流阅读器输出

c# - 如何将 MySQL blob 加载到 C# 字节数组中