CAB Service & IDisposable

Topics: CAB & Smart Client Software Factory
Feb 7, 2007 at 4:56 PM
What are the 'best practices' for using a disposable service in CAB? (In particular for my scenario, the class I'm trying to use inherits from System.ServiceModel.ClientBase which implements IDisposable)
I'd like to add our ClientBase/IDisposable service to the WorkItem.Services collection and use it. It only adds the service once, so after I use the service once and dispose it, I can't use it again.

Feb 9, 2007 at 8:50 PM
Hi wilsonhut,

I read your post. Doing some little test I could not reproduce the problem, I called my service several times without an exception. Dispose wasn't called even after I closed and disposed the view and controller.

Could you please provide more info on your scenario? I would like to know for example if you are adding the service to a particular workitem and later disposing that workitem, maybe you are trying to re use that same instance.

Any information would be of help.

Federico Delgado
Feb 12, 2007 at 2:42 PM
Thanks for your reply. I think I didn't explain myself well enough.

I didn't get an exception. I was just wanting to know the best practices. I can also call the service several times without exceptions (unless, of course, I dispose it). So are you saying that I should not dispose it after each use? By "use", I mean things I do after a user action, like search.

Do I create the service instance once and use it the entire time the screen/workItem is open? That just doesn't seem very IDisposable-friendly...
Feb 12, 2007 at 3:46 PM
"Do I create the service instance once and use it the entire time the screen/workItem is open? That just doesn't seem very IDisposable-friendly..."

Services are Singletons, so it doesn't make much sense to be creating and disposing them over and over. They're meant to be created at the highest level of object hierarchy and then injected to subsequent child WorkItems/Presenters that require access to them.
Feb 12, 2007 at 3:55 PM
The service will be disposed by the LifeTimeContainer in the object builder if you are concerned about a non-managed resource that might leak.
Feb 12, 2007 at 4:07 PM
I'm more concerned about leaving many connections open. Also, there's the issue of connection time-outs.
Feb 12, 2007 at 6:13 PM
About the open connections issue, unless you are using a long keep alive setting you should be worried since the proxy will close the connections. Anyway, if you want greater control on the services, you could use a service agent to encapsulate all the lifetime cycle and use of the service proxy, this approach however has performance penalties since you will be creating, using and disposing different proxies everytime.

Let me know if this helps,

Federico Delgado