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:
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
Is it advisable to create a channel for each and every operation?
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..:)
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With