Asynchronous programming Task
Try to use asynchronous programming (ASYNC-AWAIT) The asynchronous programming model was introduced in C#5.0 , and became very popular. ASP.NET Core uses the same asynchronous programming paradigm to make applications more reliable, faster, and more stable. You should use end-to-end asynchronous programming in your code. Let’s take an example ;We have an ASP.NET CoreMVC application,with some database operations in the middle. As we know , it may have many layers , it all depends on user’s project architecture , but let’s take a simple example , where we have Controller》Repository layers and so on. Let’s see how to write the sample code at the controller layer. [HttpGet][Route(“GetPosts”)] public async Task GetPosts(){ try { var posts = await postRepository.GetPosts();if (posts == null) { return NotFound(); /> } return Ok(posts); } catch (Exception) { return BadRequest(); } } The following code shows how we implement asynchronous programming at the repository layer. public async Task<List> GetPosts(){ if (db != null) { return await (from p in db.Postfrom c in db.Categorywhere p.CategoryId == c.Idselect new PostViewModel{ PostId = p.PostId,Title = p.Title,Description = p.Description,CategoryId = p. CategoryId,CategoryName = c.Name,CreatedDate = p.CreatedDate}).ToListAsync();} return null; } Use asynchronous programming to avoid TASK.WAIT or TAST.RESULT When using asynchronous…