简体   繁体   English

ADO.Net中的异步编程

[英]Asynchronous programming in ADO.Net

The following link http://msdn.microsoft.com/en-us/library/hh211418(v=vs.110).aspx explains the new asynchronous programming features with the 4.5 .Net framework, which for the most part is fairly straight forward reading. 以下链接http://msdn.microsoft.com/en-us/library/hh211418(v=vs.110).aspx解释了4.5 .Net框架的新异步编程功能,这在很大程度上是相当直接的前瞻性。

However, I just want to make sure of something.... 但是,我只是想确定一些事情......

If I am using a SQLDataReader in a web service and I create a service reference on the client to use that web service by generating asynchronous methods in the proxy (via the service reference dialog options), I very well imagine I wouldn't need to use the methodologies mentioned in the link above. 如果我使用的Web服务一个SqlDataReader和我创建客户端通过代理服务器产生异步方法(通过服务引用对话框选项)来使用Web服务上的服务的参考,我很想像我就不需要使用上面链接中提到的方法。

Rather, I would use the async, Task, and await keywords on the client appropriately when calling that web service method 相反,我会在调用该Web服务方法时适当地使用客户端上async,Task和await关键字

Since checking those options on the Web Service dialog, it will automatically create asynchronous method calls for the ADO.Net call. 自从在“Web服务”对话框中检查这些选项后,它将自动为ADO.Net调用创建异步方法调用。

So, if you have a method called GetCategories in the web service, it will automatically create an asynchronous call called GetCategoriesAsync in the web service which could be called from the client. 因此,如果您在Web服务中有一个名为GetCategories的方法,它将自动在Web服务中创建一个名为GetCategoriesAsync的异步调用,该调用可以从客户端调用。 Again, no need to place the asynchronous attributes on the web service method call ; 同样,不需要将异步属性放在Web服务方法调用上 ; only for an ADO.Net call which is not using a web service or one which is using a web service, but does not have the asynchronous options checked. 仅适用于没有使用Web服务或其中之一是使用Web服务,但具有异步选项选中的ADO.Net电话。

Am I correct in my thinking? 我的想法是否正确?

It depends on what you want to make asynchronous. 这取决于你想要异步的东西。

Network communications are inherently asynchronous. 网络通信本质上是异步的。 When Visual Studio creates a client proxy for your web service, it creates both asynchronous and synchronous methods (where the synchronous methods just block waiting for a response). 当Visual Studio为您的Web服务创建客户端代理时,它会创建异步和同步方法(其中同步方法只阻止等待响应)。 So, your web client can be asynchronous, and this is true regardless of how the server is implemented. 因此,您的Web客户端可以是异步的,无论服务器是如何实现的,都是如此。 In other words, a server can be synchronous or asynchronous, and a client can be synchronous or asynchronous, completely independent from each other. 换句话说,服务器可以是同步的或异步的,并且客户端可以是同步的或异步的,完全彼此独立。

On the client side, you should almost always use asynchronous methods on the proxy. 在客户端,您应该几乎总是在代理上使用异步方法。 The primary benefit on the client is responsiveness. 客户的主要好处是响应能力。

On the server side, you can get a scalability benefit by using an asynchronous implementation. 在服务器端,您可以通过使用异步实现获得可伸缩性优势。 However, if your backend is a single SQL server database, and every request hits that database, then there usually isn't a benefit from making your web service asynchronous. 但是,如果您的后端是单个SQL Server数据库,并且每个请求都会访问该数据库,那么使您的Web服务异步通常没有任何好处。 This is because (in that case) the scalability bottleneck is the SQL server, not the web server. 这是因为(在这种情况下)可伸缩性瓶颈是SQL服务器,而不是Web服务器。 OTOH, if your backend is a SQL server cluster or Azure SQL, then you can get a scalability benefit by using an asynchronous web service implementation. OTOH,如果您的后端是SQL服务器集群或Azure SQL,那么您可以通过使用异步Web服务实现获得可伸缩性优势。

The old-style common scenario was client <-> API <-> DB , and in that architecture there's no real need for asynchronous DB access . 旧式的常见场景是client <-> API <-> DB ,在该架构中, 不需要异步数据库访问 However, more and more architectures are looking like client <-> API <-> cloud storage / APIs , and that's when async really brings benefits to the API layer. 但是,越来越多的体系结构看起来像client <-> API <-> cloud storage / APIs ,而async真正为API层带来了好处。

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

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