ASP.NET Core-? Async ??? Await: ???????? ???????????? ????

ASP.NET Core-? Async ??? Await: ???????? ???????????? ????

???? ???????? ??? ??? ??? ??????? ???? ???? ???? ??????? ???? ???—????, ???? ??? ????? ???, ????? ????????? ??????? ???, ?? ???????? ????? ????????? ????? ??? ????, ???? ??? ??? ????? ?????? ??? ???? ??????? ????, ?????? ???? ??? ????? ???? ?????? ??? ???????? ???? ????? ???? ??????? ???????? ?????? ????? ???? ???? ?????? ??? ?????? ???, ??????????????? ??? ???? ????? ?????? ?????? ??? ?????? ??? ????? ??? ? ?????? ??? async ??? await?

Async ??? Await ???

async (asynchronous) ??????????????? ??????? ???? ??????? ??? ????? ???? ?????? ????????? ??? ???? ???????? ??????? ??????? ???? ?????

Async ?? ????

  • async ????????? ????? ???? ??????????? ??????? ??? ?? ?? ??????? asynchronous ??? ???????
  • ?? ??????? ????? ???? await ????????? ??????? ??? ??????????? ????? ???? ???? ????, ?????? ?? ???? ????????? ??? ???????? ????

Await ?? ????

  • await ????????? ????? ???? ??? ???? ????? ???? ??????? ???? ???, ???? ???? ?? ????? ??????? ??????????? ???? ???? ??? ???? ????


?????? ??????? Async/Await ???? ???

?????, ????? ??? ??? ????? ???? ???? ???????????? ????? ????? ?????? ???? ????? ?????? ??????? ????? ??? ??????? ???? ????? ?? ???? ?? ??? ??? ?? ????? ??? ??? ???? ???? ???? ?????? ???? ????????? ?????? ??? ??????? ???? ??????? ???? ?????

  • ????? ????? ??? async ??????? ??? ??? ????? ?? ?????? ???? ??? ????, ???????? ??? ?????? ??? ????? ???????? ?????? ???? ??????? ????? ???? ???? ?? ???? ???????? ???? ??????
  • ????????, ???? ???? synchronous ??????? ??? ??? ????? ?? ???? ????? ???? ???? ??? ????, ?? ???? ???? ??? ??? ???

??????? ?? async ???????????? ???? ???? ???????? ????????? ??? ???? ??? ????? (???? ??????? ?? API ??) ???? ??????? ????, ?? ????? ???? ??????? ??? ???? ????? ?????? ??? ???? synchronous ??? ??????? ???, ????? ??????????? ???????? ??????? ???? ??? ?????? ?? ??? ??? ??? ??? ????

ASP.NET Core-? Async/Await ????????? ??????

???? ???? ASP.NET Core-? ?????? async ??? await ??????? ??? ??? ?? ???? ???????? ??????? ?????

???? ???? ?????? ????????? ???? ??? ?? ???? ??? ????? ??????? ???? ???? ??? ????? ????? ???? async ??? await ??????? ????

using System;
using System.Net.Http;
using System.Threading.Tasks;

public class Program
{
    private static readonly HttpClient httpClient = new HttpClient();

    public static async Task<string> FetchDataFromWebServiceAsync(string url)
    {
        // HTTP GET ?????????? ??????
        HttpResponseMessage response = await httpClient.GetAsync(url);
        
        // ??? ??????? ??? ???, ????? ???????? ???? ???
        if (response.IsSuccessStatusCode)
        {
            return await response.Content.ReadAsStringAsync();
        }
        else
        {
            throw new Exception("Failed to fetch data");
        }
    }

    public static async Task Main(string[] args)
    {
        try
        {
            string url = "https://jsonplaceholder.typicode.com/posts/1"; // ???????????? ???? URL
            string data = await FetchDataFromWebServiceAsync(url);
            Console.WriteLine(data); // ???? ?????? ???????? ???
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}        

????? ????????:

  1. HttpClient: ??? ???? ????? ?? HTTP ?????????? ??????? ???? ??????? ???? ???? ??? ??????? ???? ????? (static) ????? ?????? ????? ??????
  2. FetchDataFromWebServiceAsync: ?? ?????? ???? URL ???? ??? ?????? ???? ???? ??? ???? ??? async ??????????? ??????? ????? ??? ??????, ??? ???? ?? ??? ??????????????? ??????? ???? ?????
  3. Main ????: ??? ?????? ??????????? ??????? ???????? ??? async ?????? ????? ??? ??????, ???? ???? await ??????? ???? ?????
  4. try-catch: ??? HTTP ???????????? ??? ?? ???, ????? ???? ????????? ?????? ??? ??? ??? catch ????? ??? ????

??? FetchDataFromWebServiceAsync ???? ?? ??? ???, ??? GetAsync ???????? await ???? ???? ??????? ???? ??? ??? ?? ????????, ??? Main ?????? ??????? ??? ??????? ???? ????? ??? HTTP ?????????? ??????? ???, ??? FetchDataFromWebServiceAsync ???? ???? ???? ???? ??? ??????? ???? ?????


Async ??? ?????????

?. Non-blocking: Async ??? ???????? ???????? ???? ??????????? ????? ??? ????? ?? ??? ????????? ??????? ???? ??? ??????????? ??????? ??? ?????

?. ???????????? ??????: Async ?? ??????? ASP.NET Core ???????????? ???? ???? concurrent ????? ????????? ???? ????? ????

?. ???????????? ????? ???: ??????? ?? API ???? ??? ??????? ??? async-???? ??? ???, ??? ????????? ???? ???? ?? ??? ???? ??????? ??? ???? ?????


???????

async ??? await ASP.NET Core-? ????? ?????????????? ???? ????????????? ?? ??????? ????? ???????????? ??? ???? ?????? ??? ???? ???? ??? ?????????????? ????? ????? ???? ????? ?????? ?????? ?????? ??? ?????? ????, ????? ????????????? ?????? ?????? ??? ??????? ???? ????? ???, ??? ??? ??? ???? ??? ????? ????

Async ??? await ?? ???? ??????? ??? ????? ASP.NET Core ?????????????? ??? ????????? ??? ?????!

Upama Chowdhury

Software Engineer | Innovating with ASP.NET & C# | Transforming Complexity into Clarity

1 个月

Informative

回复

要查看或添加评论,请登录

Saidur Rahman Akash的更多文章