In this case, is it bad to subscribe to the proxy CloseCompleted event?
public static void Close(this MyWCFServiceClient proxy)
{
proxy.CloseCompleted += (o, e) =>
{
if (e.Error != null)
proxy.Abort();
};
proxy.CloseAsync();
}
when the proxy is no longer referenced by any code, will it still get garbage collected, or does the event subscription in the extension method hang around holding a reference to proxy?
I wouldn’t say it is bad practice, but in the general case it should probably be obvious that this is going to happen, i.e. clearly documented in the
///markup. However, in this case we are talking about the death of an object – so the caller probably isn’t expecting to do much with the object after calling the method. I would also want to make it clear (perhaps in the method name) that this is async.Re garbage-collection; events keep the subscriber alive; not the publisher. If
proxyis eligible, then it will be collected. The anonymous method doesn’t seem to access any captured scope (except forproxyitself) – so there is nothing interesting to keep alive anyway (except for the delegate instance itself).