asp.net中同步和异步Web服务的区别 [英] Difference between synchronous and asynchronous webservices in asp.net

查看:272
本文介绍了asp.net中同步和异步Web服务的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。

任何人都可以解释一下asp.net中同步和异步Web服务之间的区别是什么?





谢谢

kiran

Hello.
Can anyone explain me what is the difference between synchronous and asynchronous webservices in asp.net with example


Thanks
kiran

推荐答案

阅读本文:synchronous-vs-asynchronous-related-to-web-services [ ^ ]


First lets clear your doubt about Synchronous and asynchronous

Synchronous communication is 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 communication does 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.







现在谈论Webservices ---









异步服务



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



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



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



例如,读取大文件可以使用其中一种异步ReadFile方法完成。



这不会阻止其中一个asp.net工作线程,从而允许更大的吞吐量。客户端通常必须以不同的方式调用此服务,使用异步关键字或轮询完成,具体取决于它的设置方式。




Now talking about Webservices---




Asynchronous service

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

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.

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

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

This will not block one of the asp.net worker threads, allowing potentially greater throughput. The client would typically have to call this service in a different fashion, either using the async keywords, or polling for completion, depending on how it's set up.


这篇关于asp.net中同步和异步Web服务的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆