{"record":{"id":"002927ae0aa98346","repo":"stride3d/stride","slug":"cannot-attach-a-project-to-more-than-one-session","errorCode":null,"errorMessage":"Cannot attach a project to more than one session","messagePattern":"Cannot attach a project to more than one session","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/PackageSession.cs","lineNumber":1387,"sourceCode":"\n                foreach (var oldProject in e.OldItems?.OfType<PackageContainer>() ?? [])\n                {\n                    UnRegisterProject(oldProject);\n                }\n\n                foreach (var projectToCopy in Projects)\n                {\n                    RegisterProject(projectToCopy);\n                }\n                break;\n        }\n    }\n\n    private void RegisterProject(PackageContainer project)\n    {\n        if (project.Session is not null)\n        {\n            throw new InvalidOperationException(\"Cannot attach a project to more than one session\");\n        }\n\n        project.SetSessionInternal(this);\n\n        if (project is SolutionProject solutionProject)\n        {\n            // Note: when loading, package might already be there\n            // TODO CSPROJ=XKPKG: skip it in a proper way? (context info)\n            if (!VSSolution.Projects.Contains(solutionProject.VSProject))\n            {\n                // Special case: let's put executable windows project first, so that Visual Studio use them as startup project (first project in .sln)\n                var insertPosition = (solutionProject.Type == ProjectType.Executable && solutionProject.Platform == PlatformType.Windows)\n                    ? 0\n                    : VSSolution.Projects.Count;\n                VSSolution.Projects.Insert(insertPosition, solutionProject.VSProject);\n            }\n        }\n","sourceCodeStart":1369,"sourceCodeEnd":1405,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/PackageSession.cs#L1369-L1405","documentation":"Thrown by PackageSession.RegisterProject when a PackageContainer project already belongs to another session (its Session property is not null). A project package can only be attached to one session at a time because the session manages its registration and save/load flow.","triggerScenarios":"Calling a session API (Load, AttachProject, Save, etc.) that internally registers a project which was already loaded into, or attached to, a different PackageSession instance.","commonSituations":"Reusing a loaded Package across two sessions (e.g. opening the same project in two sessions); loading a package file into session B after session A already owns it; not disposing the old session before creating a new one.","solutions":["Ensure the project's previous session is disposed/released before attaching it to a new one.","Load a fresh PackageContainer instance for the new session instead of reusing one.","Check project.Session before attaching: only attach when it is null or already equals the target session."],"exampleFix":"// before\nvar sessionB = new PackageSession();\nsessionB.Load(existingProject); // existingProject.Session == sessionA\n// after\nif (existingProject.Session is not null && existingProject.Session != sessionB)\n    existingProject.Session.Release(); // or use a freshly loaded PackageContainer\nsessionB.Load(existingProject);","handlingStrategy":"validation","validationCode":"if (project.Session is not null && project.Session != targetSession)\n    throw new InvalidOperationException(\"Project already belongs to another session.\");","typeGuard":"static bool CanAttach(PackageContainer project, PackageSession session) => project.Session is null || project.Session == session;","tryCatchPattern":"try { session.Load(project); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"more than one session\"))\n{\n    // release/detach from old session or load a fresh PackageContainer\n}","preventionTips":["Never reuse a loaded PackageContainer across sessions.","Dispose or release the owning session before opening the project elsewhere.","Check project.Session before any attach/load call."],"tags":["state-management","sessions","packages"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}