Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will calling close() on my WCF service release all resources?

Will calling close on my WCF service kill all resources or set them up for GC or should I set it to null also?

like image 287
Blankman Avatar asked Mar 02 '23 00:03

Blankman


2 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
like image 83
Marc Gravell Avatar answered Apr 20 '23 00:04

Marc Gravell


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?

like image 28
Brian Avatar answered Apr 19 '23 23:04

Brian