WCF 中的调用是同步的吗?
我正在使用 WCF 编写一个应用程序,其中客户端订阅服务器,然后将更新推送回客户端。
订阅者使用 DuplexPipeChannel 调用服务器上的 Subscribe() 方法来订阅服务器。
服务器维护一个List<>列表。 订阅者,当有数据要推送给订阅者时,它会调用 PushData() 方法。
我的目的是遍历订阅者列表,依次调用每个订阅者的 Push 方法。
我想知道的是:在我的订阅者上调用推送方法是否会阻塞? 连接失败或连接到其中一个订阅者的延迟是否会导致其余推送呼叫延迟(或更糟糕的失败)?
如果这是一个显而易见的问题,我很抱歉,但到目前为止我主要是 .Net 2.0 人员,所以我对 WCF 知之甚少。
我的 WCF 代码大致基于本教程。
另一个问题 假设它是同步的,我是否最好生成一个新线程来处理客户端请求,或者我是否最好为每个“推送服务器端”生成一个新线程?
I'm writing an App using WCF where clients subscribe to a server and then updates get pushed back to the clients.
The subscribers subscribe to the server using a DuplexPipeChannel calling a Subscribe() method on the server.
The server maintains a List<> of subscribers and when there is data to push out to the subscribers it calls a PushData() method.
My intention is to iterate through the list of subscribers calling the push method on each of them in turn.
What I want to know is: Is calling the push method on my Subscriber blocking? Will a failure of connectivity or delay in connecting to one of the subscribers cause the rest of the push calls to be delayed (or worse fail)?
I'm sorry if this is an obvious question, but I've been mostly a .Net 2.0 person up until now so I know very little about WCF.
My WCF code is loosly based on this tutorial.
Another Question
Assuming it is synchronous, am I better off spawning a new thread to deal with the client side requests or would I be better off spawning a new thread for each "push serverside?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,WCF 调用是同步的,但可以将它们配置为异步。 请参阅下面贾勒特的回答。 请查看此处。 您发送的每条消息都会收到返回的结果,无论您是否确实在期待数据。
该调用将根据您的服务器的操作而阻塞。 如果服务器上的 PushData 实际上遍历订阅者列表并向每个订阅者发送一条消息,那么它就会这样做。 如果 PushData 仅插入数据,而另一个线程负责将数据发送给订阅者,则它只会在服务器插入数据并返回时阻塞。
希望这可以帮助。
编辑:关于生成线程客户端与服务器端。 服务器端。 如果客户端调用需要一段时间,那就是一段时间,但如果因为服务器实际上在同一个调用中向其他客户端发送调用而需要很长时间,那么就会出现问题。 实际上我不会每次都产生一个新线程。 只需在服务器端创建一个 生产者/消费者 模式,以便每当数据项排队,消费者拿起它。 天啊,你甚至可以有多个消费者。
WCF calls are synchronous by default, although they can be configured to be asynchronous. See Jarrett's answer below. Take a look here. Every message you send will receive a result back, whether you actually are expecting data or not.
The call will block depending on what your server does. If PushData on the server actually iterates through the subscriber list and sends a message to each, it will. If PushData only inserts the data and another thread handles sending the data to the subscribers, it will only block while your server inserts the data and returns.
Hope this helps.
Edit: Regarding spawning threads client-side vs server-side. Server-side. If a client calls takes a while, that's while, but if it takes a long time because the server is actually sending out calls to other clients in the same call, then something is wrong. I would actually not really spawn a new thread each time. Just create a producer/consumer pattern on your server side so that whenever a data item is queued, the consumer picks it up. Hell, you can even have multiple consumers.
如果右键单击服务引用,您可以选择创建异步调用。 (设置对话框上有一个复选框。)我通常创建异步方法,然后监听结果。 虽然工作量更大,但我可以使用异步服务操作编写响应速度更快的应用程序。
If you right-click on the Service Reference, you have the option to create Async calls. (There's a checkbox on the setup dialog.) I usually create Async methods and then listen for a result. While it is a bit more work, I can write a much more responsive application using async service operations.