All rules
CA2024Reliability Enabled by default: As warning

Do not use StreamReader.EndOfStream in async methods

Do not use StreamReader.EndOfStream in async methods

Microsoft docs

Description

The property System.IO.StreamReader.EndOfStream can cause unintended synchronous blocking when no data is buffered. Instead, use System.IO.StreamReader.ReadLineAsync directly, which returns null when reaching the end of the stream.

Cause

A call to System.IO.StreamReader.EndOfStream is made inside an async method.

How to fix violations

To fix a violation, directly call System.IO.StreamReader.ReadLineAsync and check the return value for null.

Example

public async Task Example(StreamReader streamReader)
{
    while (!streamReader.EndOfStream)
    {
        string? line = await streamReader.ReadLineAsync();
        // Do something with line.
    }
}

public async Task Example(StreamReader streamReader)
{
    string? line;
    while ((line = await streamReader.ReadLineAsync()) is not null)
    {
        // Do something with line.
    }
}

When to suppress

You shouldn't suppress warnings from this rule, as your app might hang if you don't fix the violations.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0