dotnet/wpf · error · NotSupportedException

NotSupportedException

Error message

NotSupportedException

What it means

SiteOfOriginContainer is a read-only PackagingContainer representing the site of origin of an application. Deleting a part is not a meaningful operation for it, so DeletePartCore throws NotSupportedException by design — this is a deliberately unimplemented mutation, not a bug.

Solutions

  1. Do not delete site-of-origin parts; they represent read-only deployment content.
  2. Guard deletion with a check for the container type or part Uri scheme before calling DeletePart.
  3. Wrap deletion in try/catch for NotSupportedException when dealing with arbitrary packages.
  4. Use a real writable Package (e.g. Package.Open on a .zip) for parts that must be mutable.

Example fix

// before
package.DeletePart(partUri);
// after
if (package is SiteOfOriginContainer)
    return; // site-of-origin parts are read-only
package.DeletePart(partUri);
Defensive patterns

Strategy: try-catch

Validate before calling

if (package is SiteOfOriginContainer) return; // deletion unsupported for site-of-origin

Type guard

bool IsWritablePackage(Package p) => p is not SiteOfOriginContainer;

Try / catch

try
{
    package.DeletePart(partUri);
}
catch (NotSupportedException)
{
    // site-of-origin parts are read-only; skip
}

Prevention

When it happens

Trigger: Calling Package.DeletePart on a Package whose container is a SiteOfOriginContainer (i.e. resolving or deleting content that maps to the application's site of origin, e.g. siteOfOrigin:// URIs in an XBAP).

Common situations: Code that generically iterates pack URIs and tries to delete source parts; refactoring code that assumed all Package instances are writable ZipPackages; attempting cleanup of site-of-origin resources.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/AppModel/SiteOfOriginContainer.cs:205

        #endregion


        //------------------------------------------------------
        //
        //  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)