Will calling close on my WCF service kill all resources or set them up for GC or should I set it to null also?
3 Answers
Firstly, WCF proxies are IDisposable
, so you can kind of use using
:
using(var proxy = new MyProxy()) { // see below - not quite enough
// use proxy
}
Unfortunately, WCF also has a buggy Dispose()
implementation that regularly throws exceptions. However, here's a really cool trick to get it to work correctly. I also blogged about this myself, but I think the first link is a lot better.
So: use IDisposable
and using
, but use it with caution (in this case).
Setting a field usually makes no difference. There are a few edge-cases (such as variables captured by multiple delegates, static fields, long-life objects, etc), but in general leave it alone. In particular, do not do this, as this can theoretically extend the life:
if(field != null) field = null; // BAD

- 1,026,079
- 266
- 2,566
- 2,900
-
It sounds like OP is talking about a service, not a client, so I don't know that much of this is directly relavent (but is useful to know about WCF design choices with regards to Close/Dispose/Abort). – Brian Feb 13 '09 at 21:14
This is not so much a WCF question as a .NET question; see also
Setting Objects to Null/Nothing after use in .NET
Is disposing this object, enough? or do i need to do more?
In the Dispose(bool) method implementation, Shouldn't one set members to null?

- 963
- 13
- 26

- 117,631
- 17
- 236
- 300
-
Actually, it is - with WCF you **do** need to do more, due to the buggy Dispose() implementation. – Marc Gravell Feb 13 '09 at 20:51
You only need to set a variable to null if it's going to be reachable for a long time afterwards. Say, a field on a long-lived object, or a static field. This holds in general, not just for WCF.

- 9,976
- 1
- 39
- 82