|
| 1 | +--- |
| 2 | +title: "CA2024: Do not use StreamReader.EndOfStream in async methods" |
| 3 | +description: "Learn about code analysis rule CA2024 - Do not use StreamReader.EndOfStream in async methods" |
| 4 | +ms.date: 01/16/2025 |
| 5 | +ms.topic: reference |
| 6 | +f1_keywords: |
| 7 | + - CA2024 |
| 8 | + - DoNotUseEndOfStreamInAsyncMethodsAnalyzer |
| 9 | +helpviewer_keywords: |
| 10 | + - CA2024 |
| 11 | +author: mpidash |
| 12 | +dev_langs: |
| 13 | +- CSharp |
| 14 | +- VB |
| 15 | +--- |
| 16 | + |
| 17 | +# CA2024: Do not use StreamReader.EndOfStream in async methods |
| 18 | + |
| 19 | +| Property | Value | |
| 20 | +|-------------------------------------|------------------------------------------------------| |
| 21 | +| **Rule ID** | CA2024 | |
| 22 | +| **Title** | Do not use StreamReader.EndOfStream in async methods | |
| 23 | +| **Category** | [Reliability](reliability-warnings.md) | |
| 24 | +| **Fix is breaking or non-breaking** | Non-breaking | |
| 25 | +| **Enabled by default in .NET 10** | As warning | |
| 26 | + |
| 27 | +## Cause |
| 28 | + |
| 29 | +A call to <xref:System.IO.StreamReader.EndOfStream?displayProperty=nameWithType> is made inside an async method. |
| 30 | + |
| 31 | +## Rule description |
| 32 | + |
| 33 | +The property <xref:System.IO.StreamReader.EndOfStream?displayProperty=nameWithType> can cause unintended synchronous blocking when no data is buffered. Instead, use <xref:System.IO.StreamReader.ReadLineAsync?displayProperty=nameWithType> directly, which returns `null` when reaching the end of the stream. |
| 34 | + |
| 35 | +## How to fix violations |
| 36 | + |
| 37 | +To fix a violation, directly call <xref:System.IO.StreamReader.ReadLineAsync?displayProperty=nameWithType> and check the return value for `null`. |
| 38 | + |
| 39 | +## Example |
| 40 | + |
| 41 | +The following code snippet shows a violation of CA2024: |
| 42 | + |
| 43 | +```csharp |
| 44 | +public async Task Example(StreamReader streamReader) |
| 45 | +{ |
| 46 | + while (!streamReader.EndOfStream) |
| 47 | + { |
| 48 | + string? line = await streamReader.ReadLineAsync(); |
| 49 | + // Do something with line. |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +```vb |
| 55 | +Public Async Function Example(streamReader As StreamReader) As Task |
| 56 | + While Not streamReader.EndOfStream |
| 57 | + Dim line As String = Await streamReader.ReadLineAsync() |
| 58 | + ' Do something with line. |
| 59 | + End While |
| 60 | +End Function |
| 61 | +``` |
| 62 | + |
| 63 | +The following code snippet fixes the violation: |
| 64 | + |
| 65 | +```csharp |
| 66 | +public async Task Example(StreamReader streamReader) |
| 67 | +{ |
| 68 | + string? line; |
| 69 | + while ((line = await streamReader.ReadLineAsync()) is not null) |
| 70 | + { |
| 71 | + // Do something with line. |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +```vb |
| 77 | +Public Async Function Example(streamReader As StreamReader) As Task |
| 78 | + Dim line As String = Await streamReader.ReadLineAsync() |
| 79 | + While line IsNot Nothing |
| 80 | + ' Do something with line. |
| 81 | + line = Await streamReader.ReadLineAsync() |
| 82 | + End While |
| 83 | +End Function |
| 84 | +``` |
| 85 | + |
| 86 | +## When to suppress warnings |
| 87 | + |
| 88 | +You shouldn't suppress warnings from this rule, as your app might hang if you don't fix the violations. |
| 89 | + |
| 90 | +## Suppress a warning |
| 91 | + |
| 92 | +If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. |
| 93 | + |
| 94 | +```csharp |
| 95 | +#pragma warning disable CA2024 |
| 96 | +// The code that's violating the rule is on this line. |
| 97 | +#pragma warning restore CA2024 |
| 98 | +``` |
| 99 | + |
| 100 | +To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). |
| 101 | + |
| 102 | +```ini |
| 103 | +[*.{cs,vb}] |
| 104 | +dotnet_diagnostic.CA2024.severity = none |
| 105 | +``` |
| 106 | + |
| 107 | +For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
0 commit comments