dotnet/wpf · error · NotSupportedException

SR.PackagingWriteNotSupported

Error message

SR.PackagingWriteNotSupported

What it means

WriteableOnDemandStream.EnsureWritable throws NotSupportedException(PackagingWriteNotSupported) when the stream was opened without write intent (_wantedWrite is false). Streams created read-only must never receive Write or SetLength calls.

Solutions

  1. Check stream.CanWrite before calling Write/SetLength and open the part with FileAccess.ReadWrite or Write when modification is needed.
  2. Open the package/part in write mode (e.g. XpsDocument with PackageOpenMode.ReadWrite) before editing content.
  3. If only annotating/signing, use the dedicated writeable APIs rather than writing into a read stream.

Example fix

// before
Stream s = xpsDoc.GetPartStream(uri, FileAccess.Read);
s.Write(data, 0, data.Length); // NotSupportedException
// after
Stream s = xpsDoc.GetPartStream(uri, FileAccess.ReadWrite);
if (s.CanWrite) s.Write(data, 0, data.Length);
Defensive patterns

Strategy: type-guard

Validate before calling

if (!stream.CanWrite)
    throw new InvalidOperationException("Stream is read-only; open the part with FileAccess.ReadWrite.");

Type guard

bool IsWritable(Stream s) => s is { CanWrite: true };

Try / catch

try { stream.SetLength(newLen); }
catch (NotSupportedException)
{ ShowMessage("This document was opened read-only and cannot be modified."); }

Prevention

When it happens

Trigger: Calling Write or SetLength on a WriteableOnDemandStream obtained in read-only mode (CanWrite == false); also writing through a stream fetched with FileAccess.Read from the XPS/packaging APIs.

Common situations: Opening an XPS package part read-only (e.g. reading FixedDocuments) then attempting to modify its content stream; code that ignores the stream's CanWrite property.

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/a65d01d1a9335329. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationUI/MS/Internal/Documents/Application/WriteableOnDemandStream.cs:278

    }
    #endregion Protected Methods - Stream Overrides

    #region Private Methods
    //--------------------------------------------------------------------------
    // Private Methods
    //--------------------------------------------------------------------------

    /// <summary>
    /// Ensures either the active stream is our writeable stream or uses the
    /// factory delegate to get one and assign it as the active stream.
    /// </summary>
    /// <exception cref="System.IO.IOException" />
    /// <exception cref="System.NotSupportedException" />
    private void EnsureWritable()
    {
        if (!_wantedWrite)
        {
            throw new NotSupportedException(
                SR.PackagingWriteNotSupported);
        }

        if (!_isActiveWriteable)
        {
            Stream writer = _writeableStreamFactory(_mode, _access);
            if (writer == null)
            {
                throw new IOException(
                    SR.PackagingWriteableDelegateGaveNullStream);
            }

            if (writer.Equals(this))
            {
                throw new IOException(
                    SR.PackagingCircularReference);
            }

View on GitHub (pinned to 81131a70a4)