24

I am new to WCF. Initially I created a WCF service and used the generated client proxy to consume the service from client. So whenever I performed some operations on service everything executed sequentially as I am invoking operations synchronously. I changed the concurrency mode to multiple, but still the operations happened synchronously. Then I generated asynchronous methods for my operations and uses the begin/end patterns so which I guess "freed" the channel and let the operations happen in parallel/asynchronously increasing the throughput of my applications.

Then I used ChannelFactory to create a channel and performed the operations as the client and server can share the contracts (same project). But IClientChannel provides only BeginOpen/EndOpen/BeignClose/EndClose. It doesn't have the ClientBase's BeginOperation/EndOperation methods. So basically I cannot execute an operation asynchronously on the channel to free up so that I can use the channel to perform other operations.

The I simply created channels for every operation and it solved the problem

So my question is:

  1. Which is better (ClientBase vs. ChannelFactory) w.r.t to my scenario especially I want to perform multiple operations on the service object simultaneously with multiple threads

  2. Is it advisable to create a channel for each and every operation?

  3. In fact, I thought we can have only one channel between two endpoints (client/service). But I can create as many channels as I want. For ex: I was able to create Int16.MaxValue of channels. So not sure what the limit and recommendations on this.

    Service[] channels = new IService[Int16.MaxValue];
    
    for(int i = 0; i<Int16.MaxValue; i++)
    {
       channels[i] = factory.CreateChannel();
    }
    

So basically can you please let me know about the basics of channels and recommendations and tricks etc... etc..:)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dreamer
  • 3,371
  • 2
  • 34
  • 50

1 Answers1

28

There's a difference using async between ClientBase and ChannelFactory<T>. Basically ClientBase uses the event-driven asynchronous model.

I used ChannelFactory<T> extensively in an application I developed at work, mainly because the contracts were available in a common library for the application and I don't like using the Add Service Reference. I cache each unique instance of the ChannelFactory upon creation, and then when I need to call an operation I'll open a communication channel from that instance, make my call, and close the communication channel.

Most of the startup cost for WCF is in creation of the client, and this way you only pay it once for the life of the application - creating communication channels is trivial.

For more info on the async for ClientBase and ChannelFactory<T>, see:

How to: Call WCF Service Operations Asynchronously

How to: Call Operations Asynchronously Using a Channel Factory

Tim
  • 28,212
  • 8
  • 63
  • 76
  • Thanks Tim. Yes, I am also doing pretty much same thing. Creating a new communication channel for each operation and closing it. I looked at the links, but I still have some questions. 1. How many channels are recommended? 2. TO perform operations synchrnously using channel factory we just have to add the BeginOp/EndOp methods to channel interface. rest everything is taken care by WCF - in ohter words svcutil util generates so much code which we are not interested. we are only interested in the interface definition and let the WCF do the heavy lifting. If yes, this is cool.\ – Dreamer Oct 16 '11 at 10:12
  • Questions in addition to the ones above? – Tim Oct 16 '11 at 10:13
  • @Dreamer - I don't think there's any recommended number of channels. If you have a lot of clients you may run into concurrent connection issues, but you can adjust that in the config. As to your second question, I haven't done anything async in WCF yet (we're getting ready to do that in our next rev at work), but I would think as long as the interface is correct, you should be ok. – Tim Oct 16 '11 at 20:15
  • thanks Tim. Btw, did you get a chance to look at my other question (WCF - Creating multiple service instance for a single client based on tag/conditionally (not based on percall) – Dreamer Oct 17 '11 at 02:51