开发者

WCF - channel factory vs client base

I am new to WCF. Initially I created a W开发者_如何学编程CF 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..:)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜