8

So the question is does WCF4 invokes client connection pooling to WCF-service? For instance, we have ASP .NET application as client and service (on separate machines). Then somewhere in code we have something like that:

ServiceClient client = new ServiceClient();
// Here some work with service goes...

Lets say we have another service invoke in anither piece of code:

ServceClient client2 = new ServiceClient();
// Another one processing...

So will client2 connection taken from connection pool?

kseen
  • 359
  • 8
  • 56
  • 104
  • What kind of pool do you have in mind. You have to create client-pools by your own and implement the logic. So the question is a bit confusing. See http://blogs.msdn.com/b/wenlong/archive/2007/11/14/a-sample-for-wcf-client-proxy-pooling.aspx. – Alexander Schmidt Nov 01 '11 at 10:41
  • @sprinter252 I mean 'native pooling' what implemented by 'WCf driver'. The similar thing is ADO connection pooling. – kseen Nov 01 '11 at 10:49
  • 1
    So then the answer should be that it depends on the [ObjectPooling(MinPoolSize = 0, MaxPoolSize = 5)]-attribute defined on your service I guess. – Alexander Schmidt Nov 01 '11 at 10:57

2 Answers2

15

The "pooling" is dependent on used transport protocol. For HTTP the WCF by default uses HTTP persistent connections which live for short period of time (they are closed after 100s of inactivity) and can be reused by subsequent requests (even from different proxy instance). For TCP and Named pipes WCF provides built-in pooling.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
2

Why would you do that? WCF can accept multiple Requests over one Client with ConcurrencyMode.Multiple. So it wouldn't make much sense to initialize two Clients..

WCF ServiceContract has three important Attributes for this behaviour,

InstanceContextMode

  • PerSession (Creates per Session an Instance of the Service)
  • Single (Creates Single Instance for every Client)
  • PerCall (Creates per Call an Instance of the Service)

ConcurrencyMode

  • Multiple (Client can make multiple calls at the same time -> Multithreaded)
  • Single (Client can make one call and other have to wait until the other call finished)
  • Reentrant (Client can make multiple calls at the same time, i don't know exactly but i think it was like if one call uses another wcf service, another call can be processed until the other wcf service call is completed, so it releases the lock between time of wcf service call is made and response)

SessionMode

  • Allowed (Client can use a Session but don't have to)
  • NotAllowed (Client can't use a Session)
  • Required (Client have to use Session)

Most time I use InstanceContextMode.PerSession (Because Client 1 doesnt have access to the Variables in the Service of Client 2), ConcurrencyMode.Multiple and SessionMode.Required.

You can also specify how many Instances can be initialized, how many Concurrent Calls can be made and how many Session can be used.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
RaphaelH
  • 2,144
  • 2
  • 30
  • 43
  • You should spend more time formatting your answer. Few read badly formatted answers (and you'll therefore get fewer upvotes) – jgauffin Nov 02 '11 at 07:17
  • How is your answer related to the question? Session, instance context mode and concurrency mode has nothing to do with reusing transport connection - that is reason why connection pooling exists. Even with everything you mentioned connection pooling can be still used under the hood to improve performance. – Ladislav Mrnka Nov 02 '11 at 09:04
  • My answer is related to his Example, cause he's opening two clients.. So I thought it would be useful for him – RaphaelH Nov 02 '11 at 09:23
  • @RaphaelH My example is about two different actions in controller so it wouldn't use on client because they are in different scopes. – kseen Nov 03 '11 at 02:52
  • @RaphaelH By the way, what `InstanceContextMode` is better fo rperformance: `PerSession` or `Single`? I think it's obvious that is `Single` mode is slower than that two. – kseen Nov 03 '11 at 02:54
  • @kseen In my opinion i would say it depends on the situation.. PerSession is useful if you have in your Service Variables per Client. If you have Single, every Client has access to the same variables.. But you don't have to forget, PerSession would create for every Client another Instance of the Service, PerCall would create per Call a Instance.. That can eat up your resources! http://www.codeproject.com/KB/WCF/WCFConcurrency.aspx That's a great Article! – RaphaelH Nov 03 '11 at 06:21