dotnet/wpf · error · NotSupportedException

NotSupportedException

Error message

NotSupportedException

What it means

ResourceContainer.DeletePartCore implements the Package-internal delete hook for the read-only application resource container. Resource parts come from the compiled application assembly and cannot be removed, so any delete request throws NotSupportedException. The resource container is deliberately read-only.

Solutions

  1. Do not delete parts from the application resource package; treat it as immutable.
  2. Maintain a separate writable Package (Package.Open) for parts that need mutation.
  3. Route deletions through a wrapper that checks package type and skips resource containers.
  4. Catch NotSupportedException if generic code may target the resource package.

Example fix

// before
package.DeletePart(partUri);
// after
if (!(package is ResourceContainer))
    package.DeletePart(partUri);
Defensive patterns

Strategy: type-guard

Validate before calling

if (package is ResourceContainer)
    throw new InvalidOperationException("Application resource package is read-only");
package.DeletePart(partUri);

Type guard

static bool IsDeletablePackage(Package package) => package is not ResourceContainer;

Try / catch

try
{
    package.DeletePart(partUri);
}
catch (NotSupportedException)
{
    // resource package is read-only; use a writable package instead
}

Prevention

When it happens

Trigger: Calling Package.DeletePart (directly or via DeletePart) on the resource Package (pack://application:...) to remove an application resource part.

Common situations: Code that generically cleans up package parts assuming a writable package; attempting to 'reset' a resource by deleting it at runtime; refactored code sharing a writable-package routine with the application resource package.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/AppModel/ResourceContainer.cs:346

        #endregion Private Members

        //------------------------------------------------------
        //
        //  Uninteresting (but required) overrides
        //
        //------------------------------------------------------

        #region Uninteresting (but required) overrides

        protected override PackagePart CreatePartCore(Uri uri, string contentType, CompressionOption compressionOption)
        {
            return null;
        }

        protected override void DeletePartCore(Uri uri)
        {
            throw new NotSupportedException();
        }

        protected override PackagePart[] GetPartsCore()
        {
            throw new NotSupportedException();
        }

        protected override void FlushCore()
        {
            throw new NotSupportedException();
        }

        #endregion
    }
}

View on GitHub (pinned to 81131a70a4)