Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to close WCF client?

I've put an instance of the client proxy for the WCF service into a property on the App class so I can get it from anywhere in the app.

I am not closing the client, I'm leaving it open for the duration of the app. The main reason for this is that if I were to follow the // Comment in the WCF service mex page (the one you get if you point a browser at the WCF service url) it says // Always close the client. client.Close();

which is fine, except if I call client.Close() right after I make a call to client.SomeAsync() method then it's being closed before the results come back. Should I be putting the close into the Completed() method? Or should I just forget about closing it, as once its closed I have to create a new instance of the client proxy (might as well not store it in the App.property if that is the case.

thanks, Stephen

like image 246
Stephen Price Avatar asked Apr 27 '09 03:04

Stephen Price


1 Answers

You should close it as advised. And yes, if you're using the async methods then you have to close it only after the call completes.

Creating (opening) and closing clients is the norm for WCF clients. There is no noticeable performance penalty for continuously creating and closing new clients.

like image 169
Fung Avatar answered Oct 10 '22 10:10

Fung