2024-05-03 13:55:11 +02:00

74 lines
3.4 KiB
Plaintext

include::../../../shared_content/dotnet/csharp_dictionary.adoc[]
In an `{keyword_async}` method, any blocking operations should be avoided.
== Why is this an issue?
Using a synchronous method instead of its https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/[asynchronous] counterpart in an `{keyword_async}` method blocks the execution and is considered bad practice for several reasons:
Resource Utilization:: Each thread consumes system resources, such as memory. When a thread is blocked, it's not doing any useful work, but it's still consuming these resources. This can lead to inefficient use of system resources.
Scalability:: Blocking threads can limit the scalability of your application. In a high-load scenario where many operations are happening concurrently, each blocked thread represents a missed opportunity to do useful work. This can prevent your application from effectively handling increased load.
Performance:: Blocking threads can degrade the performance of your application. If all threads in the thread pool become blocked, new tasks can't start executing until an existing task completes and frees up a thread. This can lead to delays and poor responsiveness.
Instead of blocking, it's recommended to use the https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/await[`{keyword_async}` operator] with async methods. This allows the system to release the current thread back to the thread pool until the awaited task is complete, improving scalability and responsiveness.
== How to fix it
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
public async Task Examples(Stream stream, DbSet<Person> dbSet)
{
stream.Read(array, 0, 1024); // Noncompliant
File.ReadAllLines("path"); // Noncompliant
dbSet.ToList(); // Noncompliant in Entity Framework Core queries
dbSet.FirstOrDefault(x => x.Age >= 18); // Noncompliant in Entity Framework Core queries
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
public async Task Examples(Stream stream, DbSet<Person> dbSet)
{
await stream.ReadAsync(array, 0, 1024);
await File.ReadAllLinesAsync("path");
await dbSet.ToListAsync();
await dbSet.FirstOrDefaultAsync(x => x.Age >= 18);
}
----
== Resources
=== Documentation
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/async[async (C# Reference)]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/await[await operator - asynchronously await for a task to complete]
=== Articles & blog posts
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/#dont-block-await-instead[Asynchronous programming with async and await - Don't block, await instead]
* Microsoft Learn - https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming[Async/Await - Best Practices in Asynchronous Programming]
* Microsoft Developer Blog - https://devblogs.microsoft.com/pfxteam/asyncawait-faq/[Async/Await FAQ]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Await {identifier}Async instead.
=== Highlighting
Invoked method identifier.
endif::env-github,rspecator-view[]