dotnet/machinelearning · error · ArgumentException

StreamDoesntSupportReading

Error message

StreamDoesntSupportReading

What it means

The TextFieldParser stream constructor throws ArgumentException("StreamDoesntSupportReading") when the supplied Stream's CanRead is false. A parser cannot read delimited data from a non-readable stream, so it rejects the stream before wrapping it in a StreamReader.

Source

Thrown at src/Microsoft.Data.Analysis/TextFieldParser.cs:573

            if (defaultEncoding == null)
            {
                throw new ArgumentNullException(nameof(defaultEncoding));
            }
            string fullPath = ValidatePath(path);
            FileStream fileStreamTemp = new FileStream(fullPath, (FileMode.Open), (FileAccess.Read), (FileShare.ReadWrite));
            _reader = new StreamReader(fileStreamTemp, defaultEncoding, detectEncoding);
            ReadToBuffer();
        }

        private void InitializeFromStream(Stream stream, Encoding defaultEncoding, bool detectEncoding)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException(Strings.StreamDoesntSupportReading);
            }
            if (defaultEncoding == null)
            {
                throw new ArgumentNullException(nameof(defaultEncoding));
            }
            _reader = new StreamReader(stream, defaultEncoding, detectEncoding);
            ReadToBuffer();
        }

        private string ValidatePath(string path)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(Strings.FileNotFound);
            }
            return path;
        }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Open the stream with read access: new FileStream(path, FileMode.Open, FileAccess.Read)
  2. Verify stream.CanRead is true before constructing the parser
  3. If you intended to write data, use a different API — TextFieldParser is read-only

Example fix

// before
var fs = new FileStream(path, FileMode.Open, FileAccess.Write);
var parser = new TextFieldParser(fs); // ArgumentException
// after
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
var parser = new TextFieldParser(fs);
Defensive patterns

Strategy: validation

Validate before calling

if (stream == null || !stream.CanRead) throw new ArgumentException("TextFieldParser requires a readable stream", nameof(stream));

Try / catch

try { parser = new TextFieldParser(stream); } catch (ArgumentException ex) when (ex.Message.Contains("StreamDoesntSupportReading")) { /* reopen stream with FileAccess.Read */ }

Prevention

When it happens

Trigger: Constructing TextFieldParser(Stream) (with or without encoding args) passing a write-only stream, e.g. a FileStream opened with FileAccess.Write, a network stream used only for sending, or a Stream wrapper overriding CanRead to false.

Common situations: Reusing a stream opened for writing logs/results instead of reading; passing a stream after it was closed in a way that reports CanRead=false; confusing an output MemoryStream used for serialization with an input stream.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/53f8458ba65281d6. Report an issue: GitHub.