C#:“使用”带有 HttpWebRequests/HttpWebResponses 的语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1968522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: "Using" Statements with HttpWebRequests/HttpWebResponses
提问by Maxim Zaslavsky
Jon Skeet made a comment (via Twitter)on my SOApiDotNetcode (a .NET library for the pre-alpha Stack Overflow API):
Jon Skeet对我的SOApiDotNet代码(用于 pre-alpha Stack Overflow API 的 .NET 库)发表了评论(通过 Twitter):
@maximz2005 One thing I've noticed just from browsing the source quickly: you don't disposed (sic) of WebResponses. "using" statements FTW.
@maximz2005 我从快速浏览源代码中注意到的一件事:您没有处理(原文如此)WebResponses。“使用”语句 FTW。
He indicates that I need to wrap these Web sessions in "using" statements. However, I have a question about this: should I wrap the whole thing, starting with the HttpWebRequest, or should I create the WebRequest outside of the "using" statement and then wrap the Response inside? I have a feeling that the difference is that, in the former, both objects would be disposed of - is this correct?
他指出我需要将这些 Web 会话包装在“using”语句中。但是,我有一个关于这个问题:我应该包住整个事情,首先是HttpWebRequest的,或者我应该创建WebRequest的“使用”的声明,然后包裹响应内外部的?我有一种感觉,不同之处在于,在前者中,两个对象都将被处理 - 这是正确的吗?
Thanks in advance.
提前致谢。
采纳答案by Dzmitry Huba
HttpWebRequest
itself is not disposable unlike HttpWebResponse
. You should wrap disposable resources with using to allow early and determined cleanup. Correctly implemented IDisposable
pattern allows multiple calls to Dispose
without any issues so even the outer using statement wraps resource that during its own dispose disposes inner using statement resource it is still ok.
HttpWebRequest
本身不像是一次性的HttpWebResponse
。您应该使用 using 包装一次性资源以允许及早和确定的清理。正确实现的IDisposable
模式允许多次调用而Dispose
不会出现任何问题,因此即使外部 using 语句包装资源,在其自己的处置期间处理内部 using 语句资源它仍然可以。
Code example
代码示例
var request = (HttpWebRequest)WebRequest.Create("example.com");
using (var response = (HttpWebResponse)request.GetResponse())
{
// Code here
}
回答by Benjamin Podszun
Everything wrapped in a using () {} block (that is, inside of the first brackets) is disposed when you leave the scope.
当您离开作用域时,使用 using () {} 块(即在第一个括号内)中的所有内容都会被处理掉。
I haven't used your library so far (seems nice though), but I'd argue that you should explicitly dispose every IDisposable you create (= are responsible for) and don't return to a caller.
到目前为止,我还没有使用过你的库(虽然看起来不错),但我认为你应该明确地处理你创建的每个 IDisposable(= 负责)并且不要返回给调用者。
A sidenote, since I've seen a lot of people struggling with multiple things to dispose: Instead of
旁注,因为我看到很多人都在为处理多种事情而苦苦挣扎:而不是
using (var foo = SomeIDisposable) {
using (var bar = SomeOtherIDisposable) {
}
}
which needs a lot of vertical space you can write
这需要很多垂直空间,你可以写
using (var foo = SomeIDisposable)
using (var bar = SomeOtherIDisposable) {
}
回答by Giorgi
In order to prevent memory leaks you should call Dispose on every object that implements IDisposable. You can ensure that the Dispose method in called by using the using keyword (no pun intended) as it is just a syntactic sugar for try-finally block.
为了防止内存泄漏,您应该对实现 IDisposable 的每个对象调用 Dispose。您可以确保使用 using 关键字(无双关语)调用 Dispose 方法,因为它只是 try-finally 块的语法糖。