Net Core WebAPI

Source: Internet
Author: User

Net Core WebAPI
    • . Net Core WebAPI Task-based synchronization & Asynchronous Programming Quick Start
      • Task.result
      • Async & Await
      • Summarize

Parallel Tasks (Task) and task-based asynchronous programming (asynchronously) have been used for years in the. NET Framework and are implemented with the same functionality under Microsoft's new. NET core platform, and this article is going through. NET Core WebAPI, Describes the synchronous programming using Task.result and the asynchronous programming model using await.

Task.result

The result method can return the results of a task after it executes, as in the following code:

[HttpGet]Public Static Async task<jobject>Getjsonasync(Uri Uri) {using (var client =NewHttpClient ()) {var jsonstring =Await the client.Getstringasync (URI);Return Jobject.parse (jsonstring);}} public class mycontroller: apicontroller{ public < Span class= "Hljs-keyword" >string get (var jsontask = getjsonasync (...); return jsontask.tostring ()}}             

However, if the result method is used in ASP. Webapi to get the task output value, it will cause the current API thread to block until the task execution is complete before proceeding. It can be proved by the following code that the Get method has a thread that invokes a new thread to execute a task (Taskcaller), and when the task is executed, the execution thread of the Get method waits until the result output, because it needs to wait for the execution of the task. This thread continues the completion method.

[HttpGet]Public String Get(){var info =String.Format ("API execution Thread: {0}", Thread.CurrentThread.Managedthreadid);var infotask =Taskcaller ().Result;var infotaskfinished =String.Format ("API execution thread (after task invocation completes): {0}", Thread.CurrentThread.Managedthreadid);ReturnString.Format ("{0},{1},{2}", info, Infotask, infotaskfinished);}private async task<string> taskcaller() { await Task.  Delay (500); return string.  Format ("task execution thread: {0}", Thread. ) CurrentThread. managedthreadid);}                

Code Execution process

The output is as follows

Async & Await

If you use await, the Get Main method thread is not blocked when you call await Taskcall (), the main method thread is freed, and the new thread executes after the task finishes executing the await code to reduce the thread switching overhead, while the previous thread is idle.

[HttpGet]PublicAsync task<String>Get (){var info =String. Format ("API execution Thread: {0}", Thread.CurrentThread.ManagedThreadId);var infotask = await Taskcaller (); var infotaskfinished = string. Format (" API execution thread (after task invocation completes): {0} ", Thread.CurrentThread.ManagedThreadId); return string". Format ( "{0},{1},{2}", info, Infotask, infotaskfinished);} private async Task<string> taskcaller (await task.delay (500); return string. Format ( "task Execution thread: {0}", Thread.CurrentThread.ManagedThreadId);     

Code Execution process

The output is as follows

Summarize

The task.result with the await keyword has similar functionality to get the return value of the task, But essentially task.result will let the outer function thread block until the task executes, while the AWAIT keyword outer function thread does not block, but executes the await code through the task execution thread.

There are any errors or inaccuracies in the above content please correct me, do not like to spray!

The source of the handsome insects: http://www.cnblogs.com/vipyoumay/p/5663950.html

Net Core WebAPI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.