Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

Async and Await

0 (0 Likes / 0 Dislikes)
  • Embed Video

  • Embed normal player Copy to Clipboard
  • Embed a smaller player Copy to Clipboard
  • Advanced Embedding Options
  • Embed Video With Transcription

  • Embed with transcription beside video Copy to Clipboard
  • Embed with transcription below video Copy to Clipboard
  • Embed transcript

  • Embed transcript in:
    Copy to Clipboard
  • Invite a user to Dotsub
[Microsoft ASP.net™] [www.ASP.net] [Visual Studio 2012 & One ASP.NET] [Scott Hanselman, Principle Community Architect, Microsoft] Hi, this is Scott Hanselman. I'm going to show you a couple new features in Visual Studio 2012 and ASP.NET 4.5, specifically async and await and ASP.NET. How does asynchrony and asynchronous programming fit into the ASP.NET space? Asynchrony is built all throughout ASP.NET. You can use it in Web Forms, you can use it in MVC with asynchronous controllers, Web API, SignalR developers—anyone who is using anything at all within the ASP.NET family of technologies can use asynchronous programming. So let's talk about this. What does it look like? Here's an application. This is an ASP.NET MVC application. And we've got a pretty basic controller here. We've got a contact controller. We're going to go off to three different web services, and we're going to go and call DownloadString. DownloadString is going to use Web Client. This is pretty straightforward code that you've seen before. We'll make a call; it'll happen synchronously. When that comes back, then this line will run. And when that comes back, then this line will run. We'll throw the results into a View bag and show the view. You'll notice though that we've added a filter. We've put a stopwatch here. This is a little thing of our own—just for the example here— that's going to count how long it takes in seconds for this work to happen. So again, we're going to request this page, controller is going to run, we're going to make calls out to three different web services. Some of these might take a second or two. Some might return instantly. We'll run this application, get our contacts. And it looks like it took 2.2 seconds. We got a list of contacts, we got temperature, we got location. This is done synchronously. This is pretty standard, whether these are database calls or web services calls or file system access. The point is sometimes you have to do things and you don't know how long it's going to take for them to come back. How can we make this code asynchronous without a lot of work? Well first we can start by marking this method as async. Then instead of returning an action result, we're going to return a task action result. Then down here we're going to say await Task.WhenAll— when all of these things, when contacts and temperature and location are all done, then we'll bring those together and get the results. Except it's telling us we don't take things that aren't tasks. Well, we need to use not DownloadString, which is the standard synchronous method, but rather DownloadStringTaskAsync. All of the methods that you're used to using that could be benefitted from an asynchronous version in .NET 4.5 have those. That means file access, talking out to sockets and HTTP, any kind of low-level I/O-based API now has a task async addition. Now because we're going to await for these, in fact a task is coming back. This is no longer the data; this is the task itself because Task.WhenAll will wait for the contacts task and the temperature task and the location task to finish. The actual value is inside of result. We'll change that, so now let's look at our code. Our asynchronous controller method is going to make three calls, and those calls are going to go boom, boom, boom. They're going to return immediately because they are asynchronous. Then this thread will be freed up. This ASP.NET thread will be freed up and can be used by other requests. When everything is done, when all of those tasks are finished, then we will return, we will get the results, and we'll put them back in the View bag. Nice, easy to read—couldn't be any easier, frankly. Let's run this. Now suddenly our 2-½-second-long call took 1.1 seconds. That's because those things were happening in parallel. Some of them took a long time; some of them didn't. Remember before we said that a couple of these should take awhile. Well, in fact, contacts and temperature and location are all Web API calls. Contacts comes back immediately. But location has a Thread.Sleep. It actually take a full second to return. Temperature controller also had a sneaky Thread.Sleep there. The reason that it took us 2 seconds before is that two of those different methods were waiting for a full second. So the fastest we could have possibly come back was 2 seconds, the sum of all of those times. Now when we're doing that work asynchronously, we can come back, do them in parallel, and come back in a mere 1 second. That's how easy asynchronous programming is in ASP.NET and .NET 4.5. It's a really exciting new feature, and I hope that you have a lot of fun with it. [Microsoft ASP.NET™] [www.ASP.net]

Video Details

Duration: 5 minutes and 10 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 5
Posted by: neudesicasp on Oct 3, 2013

Async and Await

Caption and Translate

    Sign In/Register for Dotsub to translate this video.