WCF - 渠道工厂与客户群

发布于 2024-12-10 04:06:52 字数 970 浏览 0 评论 0 原文

我是 WCF 新手。最初,我创建了一个 WCF 服务,并使用生成的客户端代理来使用来自客户端的服务。因此,每当我对服务执行一些操作时,当我同步调用操作时,所有内容都会按顺序执行。我将并发模式更改为多个,但操作仍然同步发生。然后,我为操作生成了异步方法,并使用开始/结束模式,因此我猜测“释放”了通道,让操作并行/异步发生,从而增加了应用程序的吞吐量。

然后我使用 ChannelFactory 创建通道并执行操作,因为客户端和服务器可以共享合约(同一项目)。但IClientChannel仅提供BeginOpen/EndOpen/BeignClose/EndClose。它没有 ClientBaseBeginOperation/EndOperation 方法。所以基本上我无法在通道上异步执行操作来释放空间,以便我可以使用该通道执行其他操作。

我只是为每个操作创建了通道,它解决了问题

所以我的问题是:

  1. 对于我的场景,哪个更好(ClientBase 与 ChannelFactory),特别是我想在服务对象同时与多个线程

  2. 是吗建议为每个人创建一个频道以及每一个操作?

  3. 事实上,我认为两个端点(客户端/服务)之间只能有一个通道。但我可以根据需要创建任意多个频道。例如:我能够创建 Int16.MaxValue 通道。所以不确定这方面的限制和建议是什么。

    Service[] 通道 = new IService[Int16.MaxValue];
    
    for(int i = 0; i

所以基本上你可以让我了解频道的基础知识、建议和技巧等等......等等......:)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2024-12-17 04:06:52

ClientBaseChannelFactory 之间使用异步存在差异。基本上 ClientBase 使用事件驱动的异步模型。

我在工作中开发的应用程序中广泛使用了 ChannelFactory,主要是因为合约在应用程序的公共库中可用,而且我不喜欢使用“添加服务引用”。我在创建时缓存 ChannelFactory 的每个唯一实例,然后当我需要调用操作时,我将从该实例打开一个通信通道,进行调用,然后关闭通信通道。

WCF 的大部分启动成本都在于创建客户端,这样您只需在应用程序的生命周期内支付一次 - 创建通信渠道是微不足道的。

有关 ClientBaseChannelFactory 异步的详细信息,请参阅:

如何:异步调用 WCF 服务操作

如何:使用通道工厂异步调用操作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文