简体   繁体   English

谁拥有.NET中的包裹流(例如TextWriter)?

[英]Who owns wrapped streams (e.g. TextWriter) in .NET?

I've recently encountered an error "ObjectDisposedException: Cannot access a closed Stream" 我最近遇到一个错误“ObjectDisposedException:无法访问已关闭的流”

[ObjectDisposedException: Cannot access a closed Stream.]
    System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +10184402
    System.Security.Cryptography.CryptoStream.FlushFinalBlock() +114
    System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing) +48

when using code following the format: 使用以下格式的代码时:

using (var stream = new MemoryStream())
{
    using (var hashStream = new CryptoStream(stream,
                                    new SHA256Managed(), CryptoStreamMode.Write))
    using (var writer = new TextWriter(hashStream))
    {
        writer.Write("something");
    }
    // ^-- Exception occurs on hashStream Dispose
    //     While naively I assumed that TextWriter.Dispose wouldn't touch the
    //     underlying stream(s).
    return stream.ToArray();
}

So the exception is caused because the Dispose of the TextWriter Disposes the Stream (hashStream) that is wrapped. 因此引发异常是因为TextWriter的Dispose释放了包装的Stream(hashStream)。 My questions are thus: 我的问题是这样的:

  1. Does this convention applied (with default constructors/arguments) to all stream in .NET? 是否将此约定(使用默认构造函数/参数)应用于.NET中的所有流?

    Is there canon discussing this resource usage pattern? 是否有佳能讨论这种资源使用模式? For instance, can it be assumed that the CryptoStream would have closed the MemoryStream? 例如,可以假设CryptoStream会关闭MemoryStream吗? I know the answer, and there are other questions specifically about this , but I would like it addressed in terms of a design guideline if there is such. 我知道答案,并且还有其他问题 ,但如果有这样的话,我希望它在设计指南方面得到解决。

  2. Where is such behavior documented? 这种行为记录在哪里?

    I can't find "ownerships" discussed in the TextWriter(stream) or CryptoStream constructors - surely I am just looking at the wrong location. 我找不到TextWriter(stream)CryptoStream构造函数中讨论的“所有权” - 当然我只是看错了位置。 (Update: apparently I fail at reading, as pointed out by itsme86 this is documented in the TextWriter constructor documentation.) (更新:显然我不能在读书,由itsme86这 TextWriter的构造文档中介绍指出。)

  3. What is the universal accepted method to write such code? 编写此类代码的普遍接受的方法是什么?

    That is, the underlying stream needs to be read (at the end of all operations, and thus still open) while all the nested streams should be closed/flushed completely - a simple CryptoStream.Flush is not sufficient, for instance. 也就是说,底层流需要被读取(在所有操作结束时,并且因此仍然打开),而所有的嵌套流应该被关闭/ 完全冲洗-一个简单CryptoStream.Flush是不够的,例如。

This is specifically mentioned in the StreamWriter() documentation . StreamWriter()文档中特别提到了这一点。

The StreamWriter object calls Dispose() on the provided Stream object when StreamWriter.Dispose is called. 调用StreamWriter.Dispose时,StreamWriter对象在提供的Stream对象上调用Dispose()。

After reading the using statement C# spec and looking around some of the implemented streams (memory, file, etc..) i see that the default behavior is to dispose the underlying streams when calling Dispose() . 在阅读using语句C#spec并查看一些实现的流(内存,文件等)之后,我看到默认行为是在调用Dispose()时处理底层流。 There are certain streams where you can explicitly state that you dont want to dispose of the underlying stream, like in DeflateStream : 在某些流中,您可以明确声明您不想处理底层流,就像在DeflateStream

public DeflateStream(Stream stream, CompressionLevel compressionLevel, bool leaveOpen)

leaveOpen Type: System.Boolean true to leave the stream object open after disposing the DeflateStream object; leaveOpen类型:System.Boolean如果在处理DeflateStream对象后保持流对象处于打开状态,则为true; otherwise, false. 否则,错误。

Of course, you might work around the disposal by not using the using statement, or perhaps implementing a WrapperStream class which wrap your stream and doesn't dispose the underlying stream. 当然,您可以通过不使用using语句来处理处理,或者实现一个WrapperStream类来封装您的流并且不会处理底层流。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET中的DLL可以使用不同的扩展名,例如MLL吗? - Can DLL in .NET use a different extension, e.g. MLL? .NET中的默认路径(例如,在Picturebox.Load()中) - Default path in .NET (e.g. in Picturebox.Load()) 将Kinect for Windows v2的深度和颜色流另存为图像文件(例如png或jpg) - Save depth and color streams of Kinect for Windows v2 as image files(e.g. png or jpg) 如何在asp.net中获取国家/地区代码(例如+61)? - How to get country code (e.g. +61) in asp.net? 使用ASP.NET MVC2创建非ASPX视图(例如WML) - Create non aspx views (e.g. WML) with ASP.NET MVC2 类装饰器声明静态成员(例如,对于log4net)? - Class decorator to declare static member (e.g., for log4net)? 带有内置交互式终端控制台(例如SublimeText,Visual Studio)的GUI应用程序的.NET参考设计 - .NET reference design for GUI app with built-in interactive terminal console (e.g. SublimeText, Visual Studio) 远程服务器返回错误:(550) .NET 中的文件不可用(例如,找不到文件,无法访问) - The remote server returned an error: (550) File unavailable (e.g., file not found, no access) in .NET 使用Aforge.net或C#获取或设置相机属性(例如曝光时间) - Using Aforge.net or C# to get or set camera properties ( e.g. exposure time) 覆盖基类(例如.net TreeNode类)的方法,属性等有多远 - How far to go overriding methods, properties, … of a base class e.g. .net TreeNode class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM