Best Practice for WCF Service Proxy lifetime?

When working with WCF services, is it better to create a new instance of the service every time you use it? Or is it better to create one and re-use it? Why is either approach better? Is it the same for asynchronous proxies?


in addition to the things Guy Starbuck mentioned a key factor would be the security model you're using (in conjunction with the session requirements) - if you don't re-use your proxy, you can't re-use a security sessions.

This means that the client would have to authenticate itself with each call which is wasteful.

If, however, you decide this is what you wish to do, make sure to configure the client to not establish a security context (as you will never use it), this will save you a couple of roundtrips to the server :-)

Or is it better to create one and re-use it?

Do not start to implement your own pooling implementation. That has already been done in the framework. A WCF proxy uses cached channels factories underneath. Therefore, creating new proxies is not overly expensive (but see Guy Starbuck's reply regarding sessions and security!).

Also be aware that a proxy times out after a certain idle time (10mins by default).

If you want more explicit control you might consider using ChannelFactories and channels directly instead of the "easy to go, full out of the box" ClientBase proxies.

And a "must read" regarding this topic is:

One more point to consider is channel faults. By design WCF does not allow to use client proxy after unhandled exception happened.

IMyContract proxy = new MyContractClient( );
   proxy.MyMethod( );

//Throws CommunicationObjectFaultedException
proxy.MyMethod( );

Need Your Help

Posting data in PHP Curl

php post file-upload curl zoho

I am trying to post the content in a file as a param using PHP CURL.

Reading Outlook Express XP-32 DBX format and XP-64 DBX format

c++ windows-xp outlook-express

I use format/sources from to read OE6 DBX files and it works just fine. On 32-bit version of OE6, on Windows XP 32-bit.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.