简体   繁体   English

WCF 中的调用是否同步?

[英]Are calls synchronous in WCF?

I'm writing an App using WCF where clients subscribe to a server and then updates get pushed back to the clients.我正在使用 WCF 编写一个应用程序,其中客户端订阅服务器,然后将更新推送回客户端。

The subscribers subscribe to the server using a DuplexPipeChannel calling a Subscribe() method on the server.订阅者使用在服务器上调用 Subscribe() 方法的 DuplexPipeChannel 订阅服务器。

The server maintains a List<> of subscribers and when there is data to push out to the subscribers it calls a PushData() method.服务器维护一个订阅者列表<>,当有数据要推送给订阅者时,它调用 PushData() 方法。

My intention is to iterate through the list of subscribers calling the push method on each of them in turn.我的意图是遍历订阅者列表,依次调用每个订阅者的 push 方法。

What I want to know is: Is calling the push method on my Subscriber blocking?我想知道的是:在我的订阅服务器上调用 push 方法是否阻塞? 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)?连接失败或延迟连接到其中一个订阅者是否会导致推送呼叫的 rest 延迟(或更糟的是失败)?

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.如果这是一个明显的问题,我很抱歉,但到目前为止我主要是一个.Net 2.0 的人,所以我对 WCF 知之甚少。

My WCF code is loosly based onthis tutorial .我的 WCF 代码松散地基于本教程

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?"另一个问题假设它是同步的,我最好生成一个新线程来处理客户端请求还是为每个“推送服务器端”生成一个新线程更好?

WCF calls are synchronous by default, although they can be configured to be asynchronous. WCF 调用默认是同步的,尽管它们可以配置为异步的。 See Jarrett's answer below.请参阅下面的 Jarrett 的回答。 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.如果服务器上的 PushData 实际上遍历订阅者列表并向每个订阅者发送一条消息,它会。 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.如果 PushData 仅插入数据而另一个线程处理将数据发送给订阅者,则它只会在您的服务器插入数据并返回时阻塞。

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.虽然需要做更多的工作,但我可以使用异步服务操作编写响应速度更快的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM