1

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.

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.

  2. If it is necessary, then by practicing so, are we preventing the resource leak

  3. what is resource leak? Memory used up?

Thanks.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Soya
  • 115
  • 1
  • 3
  • 13
  • 1
    Generally the `.Close()` method is an alias for `.Dispose()` and hence the objects in question implement `IDisposable`. The rule is if your object implements `IDisposable` you **must** call `.Dispose()`. – Enigmativity Mar 10 '16 at 09:41

2 Answers2

2

is it necessary to call the channel.close() every time after the forms are closed and channel finishes its function.

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?

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.

Community
  • 1
  • 1
tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • 1
    Something else to note, when hosting the service on IIS, if you don't explicitly close channels when you're done, you can run out of resources on IIS in a high-volume environment, which will generate rather obscure and hard-to-diagnose at first errors. We ran into this when we deployed our first WCF service. Generally, we now use an open channel -> make request -> close/abort channel approach to minimize the time the channel is alive. – Tim Mar 10 '16 at 18:53
1

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.

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. A consumer of your type calls Dispose when the object (and the resources it uses) is no longer needed. The Dispose method immediately releases the unmanaged resources.

  2. Provide for your unmanaged resources to be released in the event that a consumer of your type forgets to call Dispose.

There are two ways to acheive option 2 above:

  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. 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. 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. 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. 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.

tom redfern
  • 30,562
  • 14
  • 91
  • 126
VVN
  • 1,607
  • 2
  • 16
  • 25
  • The next time you cut and paste your answer wholesale from MSDN, please include a link to your source and then put then entire thing in quotes. Otherwise this qualifies as plagiarism. – tom redfern Mar 10 '16 at 10:07
  • 1
    Additionally your answer does not address the OP's question, which was specifically about how to manage the disposal of WCF channel objects. – tom redfern Mar 10 '16 at 10:11
  • Guidelines on referencing other'smaterial is here: http://stackoverflow.com/help/referencing – tom redfern Mar 10 '16 at 10:12
  • Thank you for your valuable comments. – VVN Mar 10 '16 at 10:21