C#:“使用”带有 HttpWebRequests/HttpWebResponses 的语句

发布于 2024-08-16 08:24:04 字数 586 浏览 15 评论 0 原文

Jon Skeet 在我的 .googlecode.com" rel="noreferrer" title="nom">SOApiDotNet 代码(用于 pre-alpha Stack Overflow API 的 .NET 库):

@maximz2005 我注意到一件事 只需快速浏览源代码即可: 您不会处置(原文如此)WebResponses。 “使用”语句 FTW。

他指出我需要将这些 Web 会话包装在“using”语句中。但是,我对此有一个问题:我应该从 HttpWebRequest 开始包装整个事情还是应该在“using”语句之外创建 WebRequest,然后将 Response 包装在其中?我有一种感觉,区别在于,在前者中,两个对象都会被丢弃 - 这是正确的吗?

提前致谢。

Jon Skeet made a comment (via Twitter) on my SOApiDotNet code (a .NET library for the pre-alpha Stack Overflow API):

@maximz2005 One thing I've noticed
just from browsing the source quickly:
you don't disposed (sic) of WebResponses.
"using" statements 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?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ヤ经典坏疍 2024-08-23 08:24:04

HttpWebResponse 不同,HttpWebRequest 本身不是一次性的。您应该使用 using 包装一次性资源,以便尽早进行明确的清理。正确实现的 IDisposable 模式允许多次调用 Dispose,不会出现任何问题,因此即使外部 using 语句包装了在其自己的 dispose 期间处理内部 using 语句资源的资源,它仍然没问题。

代码示例

var request = (HttpWebRequest)WebRequest.Create("example.com"); 
using (var response = (HttpWebResponse)request.GetResponse()) 
{ 
    // Code here 
}

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.

Code example

var request = (HttpWebRequest)WebRequest.Create("example.com"); 
using (var response = (HttpWebResponse)request.GetResponse()) 
{ 
    // Code here 
}
复古式 2024-08-23 08:24:04

当您离开作用域时,包含在 using () {} 块中(即第一个括号内)的所有内容都会被释放。

到目前为止我还没有使用过你的库(虽然看起来不错),但我认为你应该显式地处置你创建的每个 IDisposable (= 负责)并且不要返回给调用者。

旁注,因为我看到很多人在处理多种东西时苦苦挣扎:

using (var foo = SomeIDisposable) {
  using (var bar = SomeOtherIDisposable) {
  }
}

你可以写而不是需要大量的垂直空间

using (var foo = SomeIDisposable)
using (var bar = SomeOtherIDisposable) {
}

Everything wrapped in a using () {} block (that is, inside of the first brackets) is disposed when you leave the scope.

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.

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) {
}
眼趣 2024-08-23 08:24:04

为了防止内存泄漏,您应该对每个实现 IDisposable 的对象调用 Dispose。您可以确保使用 using 关键字(无双关语)调用 Dispose 方法,因为它只是 try-finally 块的语法糖。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文