ASP.NET Core-? Async ??? Await: ???????? ???????????? ????
Saidur Rahman Akash
Software Engineer @AKIJ iBOS Limited | .NET Core, C#, MSSQL
???? ???????? ??? ??? ??? ??????? ???? ???? ???? ??????? ???? ???—????, ???? ??? ????? ???, ????? ????????? ??????? ???, ?? ???????? ????? ????????? ????? ??? ????, ???? ??? ??? ????? ?????? ??? ???? ??????? ????, ?????? ???? ??? ????? ???? ?????? ??? ???????? ???? ????? ???? ??????? ???????? ?????? ????? ???? ???? ?????? ??? ?????? ???, ??????????????? ??? ???? ????? ?????? ?????? ??? ?????? ??? ????? ??? ? ?????? ??? async ??? await?
Async ??? Await ???
async (asynchronous) ??????????????? ??????? ???? ??????? ??? ????? ???? ?????? ????????? ??? ???? ???????? ??????? ??????? ???? ?????
Async ?? ????
Await ?? ????
?????? ??????? Async/Await ???? ???
?????, ????? ??? ??? ????? ???? ???? ???????????? ????? ????? ?????? ???? ????? ?????? ??????? ????? ??? ??????? ???? ????? ?? ???? ?? ??? ??? ?? ????? ??? ??? ???? ???? ???? ?????? ???? ????????? ?????? ??? ??????? ???? ??????? ???? ?????
??????? ?? 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}");
}
}
}
????? ????????:
??? 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 ?????????????? ??? ????????? ??? ?????!
Software Engineer | Innovating with ASP.NET & C# | Transforming Complexity into Clarity
1 个月Informative