简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM