dotnet/wpf · error · InvalidOperationException
PackageAlreadyExists
Error message
PackageAlreadyExists
What it means
PackageStore.AddPackage refuses duplicate registration: if _packages already contains an entry for the given key URI, it throws InvalidOperationException(SR.PackageAlreadyExists). Each pack authority URI may map to at most one Package in the global store.
Solutions
- Call PackageStore.RemovePackage(uri) before re-adding, or check PackageStore.GetPackage(uri) != null first.
- Use a unique authority per package instance (e.g. append a version segment to the pack URI).
- Restructure startup so registration happens exactly once (guard with a flag).
Example fix
// before PackageStore.AddPackage(uri, pkg); // after if (PackageStore.GetPackage(uri) != null) PackageStore.RemovePackage(uri); PackageStore.AddPackage(uri, pkg);
Defensive patterns
Strategy: validation
Validate before calling
if (PackageStore.GetPackage(uri) != null) PackageStore.RemovePackage(uri);
Try / catch
try { PackageStore.AddPackage(uri, pkg); }
catch (InvalidOperationException ex) when (ex.Message.Contains("already exists")) { PackageStore.RemovePackage(uri); PackageStore.AddPackage(uri, pkg); } Prevention
- Check GetPackage before AddPackage
- Pair every AddPackage with RemovePackage on shutdown/reload
- Register each authority exactly once (guard with a flag)
When it happens
Trigger: Calling PackageStore.AddPackage twice with the same Uri (e.g. the same pack://myapp:,,,/ key) without removing the first package.
Common situations: Re-initialization logic that registers a resource package on every startup/event without a matching RemovePackage; plugin reload paths re-registering the same authority.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- NotAllowedPackageUri
- SR.UriMustBeAbsolute
- ' ' cannot contain the path delimiter: ' '.
- ' ' cannot start with the reserved character range…
- ' ' ID is not a valid XSD ID.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/4b15625c338ec814.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/IO/Packaging/PackageStore.cs:109
if (PackUriHelper.ComparePackUri(packUri, BaseUriHelper.PackAppBaseUri) == 0 ||
PackUriHelper.ComparePackUri(packUri, BaseUriHelper.SiteOfOriginBaseUri) == 0)
{
throw new ArgumentException(SR.NotAllowedPackageUri, nameof(uri));
}
ArgumentNullException.ThrowIfNull(package);
lock (_globalLock)
{
if (_packages == null)
{
_packages = new HybridDictionary(2);
}
if (_packages.Contains(uri))
{
throw new InvalidOperationException(SR.PackageAlreadyExists);
}
_packages.Add(uri, package);
}
}
/// <summary>
/// Removes a uri, package pair from the package store.
/// </summary>
/// <param name="uri">key uri</param>
/// <permission cref="EnvironmentPermission"></permission>
/// <remarks>
/// </remarks>
public static void RemovePackage(Uri uri)
{
ValidatePackageUri(uri);
lock (_globalLock)View on GitHub (pinned to 81131a70a4)