Ich bin verwirrend wie Asynchron und Asynchron-Zusammensetzung besonders in asp.net Kern 1Asynchron und Asynchron-Zusammensetzung in ASP.NET Core-
Dies ist Fall zu verwenden:
Dies ist Code in Controller
private readonly InfoService _infoService;
public HomeController(InfoService infoService)
{
_infoService = infoService;
}
public IActionResult Index()
{
//Create a stopwatch for getting excution time
var watch = new Stopwatch();
watch.Start();
var country = _infoService.GetCountry();
var state = _infoService.GetState();
var city = _infoService.GetCity(state);
watch.Stop();
ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds;
ViewBag.Data = city;
return View();
}
public async Task<IActionResult> About()
{
//Create a stopwatch for getting excution time
var watch = new Stopwatch();
watch.Start();
var country = _infoService.GetCountryAsync();
var state = _infoService.GetStateAsync();
var city = _infoService.GetCityAsync();
var content = await country;
var count = await state;
var name = await city;
watch.Stop();
ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds;
ViewBag.Data = name;
return View();
}
Dies ist Code in Info
public string GetCountry()
{
Thread.Sleep(3000); //Use - when you want to block the current thread.
return "VietNam";
}
public async Task<string> GetCountryAsync()
{
await Task.Delay(3000); //Use - when you want a logical delay without blocking the current thread.
return "VietNam";
}
public string GetState()
{
Thread.Sleep(5000); //Use - when you want to block the current thread.
return "the North";
}
public async Task<string> GetStateAsync()
{
await Task.Delay(5000); //Use - when you want a logical delay without blocking the current thread.
return "the North";
}
public string GetCity()
{
Thread.Sleep(6000); //Use - when you want to block the current thread.
return "HaNoi";
}
public async Task<string> GetCityAsync()
{
await Task.Delay(6000); //Use - when you want a logical delay without blocking the current thread.
return "HaNoi";
}
Run Index Aktion dauert es 14003 ms
Run über Aktion, es 6016 ms
In diesem Fall nimmt ich es parallel in über laufen denke Aktion => Gut in Async
Im Fall 2:
ich wenig Code in Über Aktion
var watch = new Stopwatch();
watch.Start();
var country = await _infoService.GetCountryAsync();
var state = await _infoService.GetStateAsync();
var city = await _infoService.GetCityAsync();
watch.Stop();
ViewBag.WatchMilliseconds = watch.ElapsedMilliseconds;
return View();
Das Ergebnis dieser Fall ändert:
Run Index Aktion, dauert es 14003 ms
Run über Aktion, dauert es etwa > 14008 ms, es jederzeit niedrig oder hoch ändert, in der Regel höher Zeit in Index Aktion
Also, was in passiert über Aktion dieses Falles?Der Unterschied in zwei Falle Über Aktion
Was passieren und Unterschied, wenn hat Multi Anfrage/Parallelität Über Aktion in zwei Fall
[I weiß, dass es nicht Thread blockieren, wenn vergleichen mit Index action]
Dies ist Fall 3:
Wie verwende ich async in Über Aktion, wenn ich
wollen- erhalten Zustand von Wert Land zurückgegeben wurde
- erhalten Stadt von Wert Land, Zustand wurde zurückgegeben
ich zur Zeit auf diese Weise verwenden:
var country = await _infoService.GetCountryAsync();
var state = await _infoService.GetStateAsync(country);
var city = await _infoService.GetCityAsync(country,state);
etwas nicht in Ordnung da drin hat? Was passiert wenn in diesem Fall mehrere gleichzeitige Anfragen vorliegen?
[Ich denke, dass es könnte entweder Wert hat oder nicht in Multi fordert gleichzeitig]
Was ist der beste Weg, async in diesem Fall zu benutzen?
Vielen Dank!
Es ist ziemlich klar, dass einer in Serie läuft, der andere parallel - was ist die Frage? – theMayer
[Immer eine gute Lektüre] (https://blog.stephencleary.com/2012/02/async-and-await.html) und [Best Practices] (https://msdn.microsoft.com/en-us/ magazine/jj991977.aspx) – MickyD
Vielen Dank für die Antwort, weil mein schlechtes Verständnis von Async speziell async in asp.net Kern, das ist Miss verstehen, wie async in Multi gleichzeitig Anfrage Umgebung ausführen. Ich habe tiefer verstehen mit der Antwort von @CodeFuller, und Link von MickyD – huanit