stride3d/stride · error · InvalidOperationException

Expecting a package that is already registered in this…

Error message

Expecting a package that is already registered in this session

What it means

PackageSession.CurrentProject's setter validates that the assigned package is one of the session's registered Projects. Assigning a Package instance loaded in (or belonging to) another session, or not loaded at all, throws InvalidOperationException because the session cannot track a project it doesn't contain.

Solutions

  1. Add the package to the session first (session.Projects.Add / the appropriate Add* API) before assigning CurrentProject
  2. Re-acquire the package from the current session instead of a stale reference
  3. Only assign packages returned by this session's load/Add APIs

Example fix

// before
var package = Package.Load(path);
session.CurrentProject = package; // throws
// after
var container = session.AddExistingProject(path, logger);
session.CurrentProject = (Package)container.Package;
Defensive patterns

Strategy: validation

Validate before calling

if (package != null && !session.Projects.Contains(package))
    throw new InvalidOperationException("Add the package to this session before assigning CurrentProject");

Type guard

bool IsRegisteredProject(PackageSession s, Package p) => p != null && s.Projects.Contains(p);

Try / catch

try { session.CurrentProject = package; }
catch (InvalidOperationException ex) when (ex.Message.Contains("already registered"))
{ logger.Error(ex, "Package does not belong to this session"); }

Prevention

When it happens

Trigger: Setting session.CurrentProject = somePackage where somePackage is not present in session.Projects (package from another session, newly created package not added, or unloaded package).

Common situations: Holding a package reference across session reloads, loading two sessions and mixing their packages, creating a Package in memory without session.AddProject/Packages.Add.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/724fcb4f345767a8. Report an issue: GitHub.

Appendix: source

Thrown at sources/assets/Stride.Core.Assets/PackageSession.cs:633

    /// <summary>
    /// Gets or sets the selected current package.
    /// </summary>
    /// <value>The selected current package.</value>
    /// <exception cref="System.InvalidOperationException">Expecting a package that is already registered in this session</exception>
    public SolutionProject CurrentProject
    {
        get
        {
            return currentProject;
        }
        set
        {
            if (value is not null)
            {
                if (!Projects.Contains(value))
                {
                    throw new InvalidOperationException("Expecting a package that is already registered in this session");
                }
            }
            currentProject = value;
        }
    }

    /// <summary>
    /// Gets the packages referenced by the current package.
    /// </summary>
    /// <returns>IEnumerable&lt;Package&gt;.</returns>
    public IEnumerable<Package> GetPackagesFromCurrent()
    {
        if (CurrentProject.Package is null)
        {
            yield return CurrentProject.Package;
        }

        foreach (var dependency in CurrentProject.FlattenedDependencies)

View on GitHub (pinned to 96fad776d2)