dotnet/wpf · error · NotSupportedException

Stream does not support writing.

Error message

Stream does not support writing.

What it means

NetStream is a read-only stream used to read package parts over the network; Write is intentionally unimplemented and always throws NotSupportedException (SR.WriteNotSupported). This library throws it because the stream has no writable backing store.

Solutions

  1. Only read from NetStream; write through the Packaging APIs (PackagePart stream opened with FileMode.Create) instead
  2. Check stream.CanWrite before invoking Write and route writes to a different stream
  3. Copy the NetStream into a writable stream if mutation is required

Example fix

// before
netStream.Write(data, 0, data.Length); // throws
// after
if (netStream.CanWrite)
    netStream.Write(data, 0, data.Length);
else
    writablePartStream.Write(data, 0, data.Length);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!stream.CanWrite)
    throw new InvalidOperationException("Target stream is read-only; open the package part for write access instead");

Type guard

static bool IsWritable(Stream s) => s.CanWrite;

Try / catch

try { stream.Write(data, 0, data.Length); } catch (NotSupportedException) { // route to a writable PackagePart stream
    part.GetStream(FileMode.Create, FileAccess.Write).Write(data, 0, data.Length); }

Prevention

When it happens

Trigger: Calling Write(byte[], int, int) — or BeginWrite/any copy routine that writes — on a NetStream obtained from a package part request; thrown unconditionally.

Common situations: Generic stream helpers that write to both source and destination (tee/logging wrappers); code that assumed GetResponseStream-like streams were writable; switching a MemoryStream for a NetStream in shared code.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/91db18d088f516db. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/NetStream.cs:311


        /// <summary>
        /// SetLength
        /// </summary>
        /// <exception cref="NotSupportedException">not supported</exception>
        public override void SetLength(long newLength)
        {
            throw new NotSupportedException(SR.SetLengthNotSupported);
        }


        /// <summary>
        /// Write
        /// </summary>
        /// <exception cref="NotSupportedException">not supported</exception>
        public override void Write(byte[] buf, int offset, int count)
        {
            throw new NotSupportedException(SR.WriteNotSupported);
        }


        /// <summary>
        /// Length
        /// </summary>
        public override long Length
        {
            get
            {
                CheckDisposed();

                // handle ftp servers that don't return a length
                if (_fullStreamLength < 0)
                {
                    // fallback for servers that refuse to provide the length of the resource
                    checked
                    {

View on GitHub (pinned to 81131a70a4)