C# 扩展 SoapExtension -
背景: 我正在尝试编写一个简单的 SoapExtension 类来记录来自 asmx Web 服务的入站/出站 Soap 消息。按照 msdn 上的这篇文章,我已经能够让事情正常运转。然而,我真的很想了解它为什么/如何工作,而不仅仅是复制和复制。粘贴代码。
问题: 我正在努力具体掌握示例中 IO 流的处理。我在网上读过的所有其他文章都以相同的方式处理流......首先获取对原始流的引用,创建内存中的“工作”流,然后根据需要交换内容。
第一个问题是,在这种情况下“流链接”是什么意思?我对流的理解是,写入任何流都会自动写入管道中的“内部”流。如果是这样,为什么需要手动将内容从一个流复制到另一个流?
第二个问题是,在示例 Copy 方法中,他们每次都会创建 StreamReader 和 StreamWriter,但不会释放它们 - 这是否会给 GC 带来额外的压力?这似乎不是您在高流量 Web 服务上想要的东西...我尝试将两者包装在 using 语句中,但处理读取器/写入器也关闭了流,这导致了更严重的错误。 .NET 4 具有新的 Stream.CopyTo(Stream) 方法,但对于 .NET 3.5 来说,更好的方法是什么?
Background:
I'm trying to write a simple SoapExtension class to log inbound/outbound Soap messages from an asmx web service. Following this article on msdn, I have been able to get things working. However I'd really like to understand why/how it's working rather than just copy & pasting code.
The question:
What I'm stuggling to grasp specifically is the handling of the IO streams in the example. All other articles I've read on the web handle the streams in an identical way... first getting a reference to the original stream, creating an in memory "working" stream, and then swapping the contents as necessary.
First question is, what is meant by "stream chaining" in this context? My understaning of streams is that writing to any stream will automatically write to the 'inner' streams in a pipeline. If that's the case, why is it necessary to manually copy contents from one stream to another?
Second question is, in the examples Copy method they're creating a StreamReader and StreamWriter each time, without disposing them - is this not putting extra pressure on the GC? Doesn't seem like something you'd want on a high traffic web service... I tried wrapping both in using statements, but disposing the reader/writer also closed the stream which led to more serious errors. .NET 4 has new Stream.CopyTo(Stream) methods, but what would be a better approach for .NET 3.5?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,通过链接流,您基本上可以让不同的流以链接顺序执行不同的操作。例如,您可以使用一个流来压缩数据,然后使用另一个流来加密数据(如果我们朝另一个方向移动,则相反)。
至于 ChainStream 本身,嗯……关于这个有很多话要说。我真的推荐这篇文章Inside of Chainstream,非常深入,也涵盖了您遇到的大部分问题。
Well, by chaining streams you can basically have different streams that do different things, in a chained sequence. For instance, you can have one stream that compresses the data, and then another stream that encrypts the data (or the opposite if we are moving in the other direction).
As for ChainStream itself, well... There are lots of things to say about this one. I really recommend this article called Inside of Chainstream, which is extremely in-depth and also covers most of the questions you have.
链接是在框架中完成的。您获取原始流并返回放置修改结果的流。该框架会将这个新流链接到任何其他扩展中。
它是这样实现的,因为链接是“向后”工作的。通常,您会在流之上添加新功能,但在这种情况下,您需要处理输入到原始流中的信息。
在流上调用 close 与 Dispose 相同。
The chaining is done in the framework. You get the original stream and return the stream where you put your modified result. The framework will chain this new stream into any other extensions.
It is implemented this way because the chaining works "backwards". Normally you add new functionality on top of streams but in this case you want to deal with the information fed into the original stream.
Calling close on stream is the same as Dispose.