Response.Close() 和 Response.Dispose() 有什么区别?

发布于 2024-10-09 07:46:51 字数 75 浏览 0 评论 0原文

从资源清理的角度来看,为什么有 Response.Close() 和 Response.Dispose() ,哪个更全面(调用另一个)?

From the resource clean-up perspective, why there are Response.Close() and Response.Dispose() and which one is more comprehensive (call the other one) ?

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

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

发布评论

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

评论(3

短叹 2024-10-16 07:46:51

如果提供了两种方法,则 Dispose 的实现应调用 Close。最好使用 using 语句来确保调用 Dispose 并因此调用 Close,即使存在异常也是如此。

换句话说,这样做:

using (Response response = ...)
{
    // ...
}

不是这样:

Response response = ...;
// ...
response.Close(); // If there is an exception this might never get called!

关闭和处置一个对象之间的一个区别是,当您处置一个对象时,通常不可能再使用该对象(尝试这样做可能会导致抛出 ObjectDisposedException),但是调用 Close 后,仍有可能使用该对象。

请注意,如果您正在谈论 ASP.NET,则通常不应在 Response 对象上调用 Close 或 Dispose。

Where both methods are provided the implementation of Dispose should call Close. It is a good idea to use the using statement to ensure that Disposeand therefore Close is called, even if there is an exception.

In other words do this:

using (Response response = ...)
{
    // ...
}

Not this:

Response response = ...;
// ...
response.Close(); // If there is an exception this might never get called!

One difference between closing and disposing an object is that when you dispose an object it usually is not possible to use the object any more (attempting to do so may cause an ObjectDisposedException to be thrown), but after calling Close it may be possible to still use the object.

Note that if you are talking about ASP.NET then you shouldn't normally call Close or Dispose on the Response object.

筑梦 2024-10-16 07:46:51

来自《开发类库设计指南》中的实现 Finalize 和 Dispose 以清理非托管资源< /a>

有时,特定于域的名称是
比 Dispose 更合适。为了
例如,文件封装可能
想要使用方法名称 Close。在
这种情况下,私下执行Dispose
并创建一个公共 Close 方法
调用 Dispose。下面的代码
示例说明了这种模式。你
可以用方法名称替换 Close
适合您的域。这
示例需要系统命名空间。

/ Do not make this method virtual.
// A derived class should not be allowed
// to override this method.
public void Close()
{
   // Call the Dispose method with no parameters.
   Dispose();
}

通常,每当可以打开或重新打开资源时,我都会看到 close,因为它为方法名称提供了很好的对称性。

From the Design Guidelines for Developing Class Library on Implementing Finalize and Dispose to Clean Up Unmanaged Resources

Occasionally a domain-specific name is
more appropriate than Dispose. For
example, a file encapsulation might
want to use the method name Close. In
this case, implement Dispose privately
and create a public Close method that
calls Dispose. The following code
example illustrates this pattern. You
can replace Close with a method name
appropriate to your domain. This
example requires the System namespace.

/ Do not make this method virtual.
// A derived class should not be allowed
// to override this method.
public void Close()
{
   // Call the Dispose method with no parameters.
   Dispose();
}

Typically I've seen close whenever the resource can be opened or re-opened, since it gives nice symmetry to the method names.

不回头走下去 2024-10-16 07:46:51

Response.Close() 关闭与客户端的套接字连接。
Response.Dispose() 是实现 IDisposable 接口并释放分配资源的方法。

我认为 Response.Close() 是从 Response.Dispose() 方法调用的。

有关更多详细信息,您可以使用 Reflector

Response.Close() closes the socket connection to a client.
Response.Dispose() is method which implements IDisposable interface and releases allocated resources.

I think Response.Close() is called from Response.Dispose() method.

For more detailed information you can use Reflector

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