C# 与 Web 服务相关的同步与异步

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17872854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:42:12  来源:igfitidea点击:

Synchronous Vs Asynchronous related to web services

c#asp.netweb-servicesasynchronous

提问by Jake

I was trying to find a good explanation for the difference between synchronous communication vs asynchronous communication for web services, all over the internet. but it seems that even the people who is willing to provide an answer to the problem is also confused with it. and one answer is the complete vice versa for another answer.

我试图为整个互联网上的 Web 服务的同步通信与异步通信之间的区别找到一个很好的解释。但似乎即使是愿意为问题提供答案的人也对此感到困惑。一个答案是完整的,反之亦然。

If anybody can provide a good explanation about the difference of above matter with a clear idea, it would be helpful for everybody who would face the same problem in the future.

如果有人能以清晰的思路很好地解释上述问题的差异,对以后遇到同样问题的每个人都会有所帮助。

采纳答案by Paddy

Asynchronous service

异步服务

Say you have a long running web service (say it reads a large file from the file system and does some processing).

假设您有一个长时间运行的 Web 服务(假设它从文件系统读取一个大文件并进行一些处理)。

If you set this up as a 'synchronous' web service (using the WCF definition of that), then the calling client will have to wait until the processing completes, and typically this will block on one of the asp.net worker threads while processing completes. For a service with high traffic, this can become problematic.

如果您将其设置为“同步”Web 服务(使用它的 WCF 定义),则调用客户端将必须等到处理完成,并且通常这将在处理时阻塞在一个 asp.net 工作线程上完成。对于高流量的服务,这可能会成为问题。

If you set this up as an asynchronous web service, then what you are saying is that your code is going to delegate some of the long running processing to another thread, or use a non-blocking mechanism, and that this will return at some point in the future (if you are using c# 5.0, then you might want to look at examples of the async and await keywords).

如果您将其设置为异步 Web 服务,那么您的意思是您的代码会将一些长时间运行的处理委托给另一个线程,或者使用非阻塞机制,并且这将在某个时候返回将来(如果您使用的是 c# 5.0,那么您可能需要查看 async 和 await 关键字的示例)。

For the example, reading a large file could be done using one of the async ReadFile methods.

例如,可以使用异步 ReadFile 方法之一来读取大文件。

This will not block one of the asp.net worker threads, allowing potentially greater throughput.

这不会阻塞一个 asp.net 工作线程,从而允许更大的吞吐量。

(There is often some confusion when people refer to making multiple simultaneous calls to the same service (often via AJAX from a web page) - while the calls from the page will typically be made using an asynchronous mechanism in javascript, this is not quite the same as what is described above - I like to keep a distinction between multiple parallel calls and asynchronous calls in my head)

(当人们提到对同一个服务进行多个同时调用(通常通过网页中的 AJAX)时,通常会有些混淆——虽然页面调用通常是使用 javascript 中的异步机制进行的,但这并不完全是与上面描述的相同 - 我喜欢在脑海中区分多个并行调用和异步调用)

Asynchronous calls

异步调用

It's also worth noting that you can make an asynchronous call to a service even if that service is not set up to be 'asynchronous'. This is how AJAX calls in javascript will work, e.g.

还值得注意的是,即使该服务未设置为“异步”,您也可以对服务进行异步调用。这就是 javascript 中的 AJAX 调用的工作方式,例如

var jqxhr = $.ajax( "AnyService.svc" )
    .done(function() { alert("success"); })
    .fail(function() { alert("error"); })
    .always(function() { alert("complete"); });

alert("Called");

For this example, you would expect to see 'Called' displayed before 'Success', as this will not wait for the service to return prior to proceeding. The service you are calling does not have to be 'asynchronous'.

对于此示例,您希望在“成功”之前看到“已调用”,因为这不会等待服务返回后再继续。您正在调用的服务不必是“异步的”。

Edit

编辑

As pointed out in the comments, you can also have a client that calls an 'asynchronous' service in a synchronous manner (i.e. the service will not block worker threads for further requests, but the client will block at that side).

正如评论中指出的那样,您还可以拥有一个以同步方式调用“异步”服务的客户端(即该服务不会阻止工作线程以进行进一步的请求,但客户端将在该端阻止)。

回答by Jake

well you are not very specific with your problem, but this might help: at the most basic level synchronous communication is when the communication with the server takes place at the same time that other requests for other assets are sent. for example an ajax call will block other assets from downloading if it is synchronous.

好吧,您对您的问题不是很具体,但这可能会有所帮助:在最基本的层面上,同步通信是在与服务器进行通信的同时发送对其他资产的其他请求。例如,如果是同步的,ajax 调用将阻止其他资产的下载。

asynchronous communication is when the communication is independant of other assets in the web service/page. this sort of communication is used more often.

异步通信是指通信独立于 Web 服务/页面中的其他资产。这种沟通方式使用得更频繁。

回答by Nipun Ambastha

First lets clear your doubt about Synchronous and asynchronous

首先让我们清除您对同步和异步的疑虑

Synchronous communicationis direct communication where the communicators are time synchronized. This means that all parties involved in the communication are present at the same time. This includes, but is not limited to, a telephone conversation (not texting), a company board meeting, a chat room event and instant messaging.

同步通信是通信器时间同步的直接通信。这意味着参与通信的所有各方都同时在场。这包括但不限于电话交谈(非短信)、公司董事会会议、聊天室活动和即时消息。

Asynchronous communicationdoes not require that all parties involved in the communication to be present at the same time. Some examples are e-mail messages, discussion boards, blogging, and text messaging over cell phones. In distance (specifically online) education asynchronous communication is the major (sometimes the only) method of communication. Usually, we use different discussion boards in each class with each having its own purpose.

异步通信不要求参与通信的所有各方同时在场。一些示例是电子邮件消息、讨论板、博客和通过手机发送的文本消息。在远程(特别是在线)教育中,异步通信是主要的(有时是唯一的)通信方法。通常,我们在每个班级中使用不同的讨论区,每个讨论区都有自己的目的。

e.g. synchronous

例如 同步

When I call you on the phone, I dial your number and WAIT until you pick up. Then you say something, and in the very same moment I listen to you. When you finished, I send you data (talk to you) and in the same moment you receive them (listen to me). At the end of our communication one of us says "END OF TRANSMISSION" (Good Bye), the other says "Acknoledged" (Good Bye) and then both ring off.

当我打电话给你时,我会拨你的号码,然后等你接听。然后你说点什么,就在同一时刻,我听你说话。完成后,我会向您发送数据(与您交谈),并在您收到它们的同时(听我说)。在我们的通信结束时,我们中的一个说“传输结束”(再见),另一个说“确认”(再见),然后双方都响了。

asynchronous

异步

I write you a letter. I put it to the postoffice, and it will be sent to you. I the meantime I do NOT WAIT. I do many different other things. Then you receive the letter. You read it while I still do many different other things. Then you write me an answer and send it to me. In all those things I am not involved. At the next day I get a (synchronous) message (a signal) from the system (postman). It (he) says: "Here is a message for you". Alternatively I could poll my inbox every five minutes to check if a new letter is there. Then I pause my other work, receive your letter and read your answer. Then I do something according to this answer. But this are things you will not notice, because you are not involved in what I do with your asynchronous answer.

我给你写信。我把它寄到邮局,它会寄给你。我同时我不等待。我做许多不同的其他事情。然后你会收到这封信。你读它的时候我还在做许多不同的事情。然后你给我写一个答案并发送给我。所有这些事情我都没有参与。第二天,我从系统(邮递员)收到(同步)消息(信号)。它(他)说:“这是给你的消息”。或者,我可以每五分钟轮询我的收件箱以检查是否有新信件。然后我暂停我的其他工作,收到你的来信并阅读你的回答。然后我根据这个答案做一些事情。但这是你不会注意到的事情,因为你没有参与我对你的异步回答所做的事情。

courtesy: How does Synchronous and Asynchronous communication work exactly

礼貌:同步和异步通信是如何工作的

回答by RakeshS

Synchronous - You are making a call to a friend, He picked up the call and responded to you. Asynchronous - You have sent a text to your friend on his mobile, But your friend might reply instantly or may reply 10mins after or may be after 2 days. This case you don't expect a instant answer from your friend.

同步 - 您正在给朋友打电话,他接听电话并回复了您。异步 - 您已经在他的手机上向您的朋友发送了一条短信,但您的朋友可能会立即回复,也可能会在 10 分钟后回复,也可能会在 2 天后回复。在这种情况下,您不希望朋友立即回答。