asynchronous web services writing asynchronous web services softuni team technical trainers software...
TRANSCRIPT
![Page 1: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/1.jpg)
Asynchronous Web ServicesWriting Asynchronous
Web Services
SoftUni TeamTechnical TrainersSoftware Universityhttp://softuni.bg
Web Services & Cloud
![Page 2: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/2.jpg)
Table of Contents
1. ASP.NET Thread Pool Synchronous Model Asynchronous Model
2. Asynchronous Actions async and await
2
![Page 3: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/3.jpg)
ASP.NET Thread Pool3
![Page 4: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/4.jpg)
ASP.NET has a dedicated thread pool for servicing requests Each request is granted its own thread to execute on
After the request is serviced, the thread returns to the thread pool and is ready to process a new request
ASP.NET Thread Pool
Thread PoolASP.NET
HTTP Server Requests
![Page 5: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/5.jpg)
5
A large number of requests can cause thread starvation Not enough threads from the thread pool to service all requests Requests queue up and
wait for a thread to become available
Thread Starvation
![Page 6: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/6.jpg)
6
The servicing thread mostly "waits" for something to happen E.g. database query result,
hard-disk read, networkresponse, etc.
During this time it does noactual work
Synchronous Actions
public IHttpActionResult GetAllAds( [FromUri]GetAdsBindingModel model){ ... var data = this.Context.Ads .OrderByDescending(ad => ad.Name) .Skip(page * model.Number) .Take(model.Number) .Select(ad => ad.Name) .ToList();
return this.Ok(data);}
Thread is blocked until the database returns the result
![Page 7: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/7.jpg)
7
The thread leaves a request whenever long waiting is possible Services other requests during this time Returns when the waiting is over
Asynchronous Model
![Page 8: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/8.jpg)
8
.NET 4.5 makes it very easy to write asynchronous actions Method is marked async Return type is wrapped
in a Task<> Blocking operation isawait-ed
Asynchronous Actions
public async Task<IHttpActionResult> GetAllAdsAsync( [FromUri]GetAdsBindingModel model){ ... var data = await this.Context.Ads .OrderByDescending(ad => ad.Name) .Skip(page * model.Number) .Take(model.Number) .Select(ad => ad.Name) .ToListAsync();
return this.Ok(data);}
Thread is freed until the *Async() method
returns a result
![Page 9: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/9.jpg)
Asynchronous ActionsLive Demo
9
![Page 10: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/10.jpg)
?
??
?
?
??
?
?
Questions?
https://softuni.bg/courses/web-services-and-cloud/
Asynchronous Web Services
![Page 11: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/11.jpg)
License
This course (slides, examples, demos, videos, homework, etc.)is licensed under the "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" license
11
Attribution: this work may contain portions from "Web Services and Cloud" course by Telerik Academy under CC-BY-NC-SA license
![Page 12: Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University](https://reader035.vdocuments.mx/reader035/viewer/2022062423/56649f4a5503460f94c6be12/html5/thumbnails/12.jpg)
Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education,
Profession and Job for Software Developers softuni.bg
Software University @ Facebook facebook.com/SoftwareUniversity
Software University @ YouTube youtube.com/SoftwareUniversity
Software University Forums – forum.softuni.bg