简体   繁体   English

我们是否需要处理资源以及为什么在WCF中需要

[英]Do we need Disposing of resources and why do we need in WCF

I realized that in some of the WPF code, we are opening the channels, some will be closed by channel.close() method, some are not, in practice. 我意识到,在某些WPF代码中,我们正在打开通道,实际上,某些通道将通过channel.close()方法关闭。

What i want to know is: 我想知道的是:

  1. is it necessary to call the channel.close() every time after the forms are closed and channel finishes its function. 每次在窗体关闭并且通道完成其功能之后是否有必要每次调用channel.close()

  2. If it is necessary, then by practicing so, are we preventing the resource leak 如果有必要,那么通过实践,我们是否可以防止资源泄漏

  3. what is resource leak? 什么是资源泄漏? Memory used up? 内存用完了吗?

Thanks. 谢谢。

It's a good practice. 这是一个好习惯。 The below is taken from here 下面是从这里取的

For the majority of the objects that your app creates, you can rely on the .NET Framework's garbage collector to handle memory management. 对于您的应用程序创建的大多数对象,您可以依靠.NET Framework的垃圾收集器来处理内存管理。

However, when you create objects that include unmanaged resources , you must explicitly release those resources when you finish using them in your app. 但是,创建包含非托管资源的对象时,必须在应用程序中使用完这些资源后显式释放这些资源。 The most common types of unmanaged resource are objects that wrap operating system resources, such as files, windows, network connections, or database connections. 最常见的非托管资源类型是包装操作系统资源的对象,例如文件,窗口,网络连接或数据库连接。

Although the garbage collector is able to track the lifetime of an object that encapsulates an unmanaged resource, it doesn't know how to release and clean up the unmanaged resource. 尽管垃圾收集器能够跟踪封装了非托管资源的对象的生存期,但它不知道如何释放和清理非托管资源。

If your types use unmanaged resources, you should do the following: 如果您的类型使用非托管资源,则应执行以下操作:

  1. Implement the dispose pattern. 实施处置模式。 This requires that you provide an IDisposable.Dispose implementation to enable the deterministic release of unmanaged resources. 这要求您提供一个IDisposable.Dispose实现,以实现确定性释放非托管资源。 A consumer of your type calls Dispose when the object (and the resources it uses) is no longer needed. 当不再需要对象(及其使用的资源)时,这种类型的使用者将调用Dispose。 The Dispose method immediately releases the unmanaged resources. Dispose方法立即释放非托管资源。

  2. Provide for your unmanaged resources to be released in the event that a consumer of your type forgets to call Dispose. 如果您的使用者忘记致电Dispose,则可以释放您的非托管资源。

There are two ways to acheive option 2 above: 有两种方法可以实现上述选项2:

  1. Use a safe handle to wrap your unmanaged resource. 使用安全句柄包装您的非托管资源。 This is the recommended technique. 这是推荐的技术。 Safe handles are derived from the System.Runtime.InteropServices.SafeHandle class and include a robust Finalize method. 安全句柄是从System.Runtime.InteropServices.SafeHandle类派生的,并包括一个健壮的Finalize方法。 When you use a safe handle, you simply implement the IDisposable interface and call your safe handle's Dispose method in your IDisposable.Dispose implementation. 使用安全句柄时,只需实现IDisposable接口,并在IDisposable.Dispose实现中调用安全句柄的Dispose方法。 The safe handle's finalizer is called automatically by the garbage collector if its Dispose method is not called. 如果未调用安全句柄的终结器,则垃圾回收器将自动调用该终结器。

  2. Override the Object.Finalize method. 重写Object.Finalize方法。 Finalization enables the non-deterministic release of unmanaged resources when the consumer of a type fails to call IDisposable.Dispose to dispose of them deterministically. 当类型的使用者未能调用IDisposable时,通过终结处理可以非确定性地释放非托管资源,请处置性地确定性处置它们。 However, because object finalization can be a complex and error-prone operation, we recommend that you use a safe handle instead of providing your own finalizer. 但是,由于对象终结处理可能是一个复杂且容易出错的操作,因此建议您使用安全的句柄,而不要提供自己的终结器。

is it necessary to call the channel.close() every time after the forms are closed and channel finishes its function. 每次在窗体关闭并且通道完成其功能之后是否有必要每次调用channel.close()。

You should always try to dispose of your client channels. 您应该始终尝试处置客户渠道。 The correct way to do this is well discussed here: What is the best workaround for the WCF client `using` block issue? 此处讨论了正确的方法: WCF客户端“使用”块问题的最佳解决方法是什么?

If it is necessary, then by practicing so, are we preventing the resource leak 如果有必要,那么通过实践,我们是否可以防止资源泄漏

Yes

what is resource leak? 什么是资源泄漏? Memory used up? 内存用完了吗?

Yes that is correct. 对,那是正确的。 The resource is not properly disposed so it remains in memory until the appdomain is unloaded. 资源未正确处置,因此它将保留在内存中,直到卸载appdomain。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM