views:

1437

answers:

2

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

+3  A: 

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.

fung
A: 

Explicitly close it preferably in a finally{} of your Completed method. For whatever reason if the client stays connected you will start blocking other client calls. As per the HTTP RFC:

Defined in 1999 (RFC 2616) “clients that use persistent connections should limit the number of simultaneous connections that they maintain to a given server. A single-user client SHOULD NOT maintain more than 2 connections with any server or proxy. A proxy SHOULD use up to 2*N connections to another server or proxy, where N is the number of simultaneously active users. These guidelines are intended to improve HTTP response times and avoid congestion.” Since developers are using AJAX or AJAX-like requests to update a Web page the http limits are discussed more and more.

With a load if you are not closing your connections it is very possible you will start blocking. I am of course assuming this is HttpBinding.

Chad Grant