Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF - channel factory vs client base

Tags:

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..:)

like image 734
Dreamer Avatar asked Oct 16 '11 09:10

Dreamer


People also ask

What is Channel factory in WCF?

A Channel Factory enables you to create a communication channel to the service without a proxy. A Channel Factory that creates and manages the various types of channels which are used by a client to send a message to various configured service endpoints.

What is a service proxy in WCF?

A WCF proxy is a CLR class that exposes the service contract. A Service proxy class has the service contract operations and some additional operations for managing the proxy life cycle and the connection to the service.


1 Answers

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

like image 106
Tim Avatar answered Oct 13 '22 22:10

Tim