{"record":{"id":"4cd4c244a8c697cf","repo":"dotnet/wpf","slug":"sr-annotationservicealreadyexists","errorCode":null,"errorMessage":"SR.AnnotationServiceAlreadyExists","messagePattern":"SR\\.AnnotationServiceAlreadyExists","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationService.cs","lineNumber":1037,"sourceCode":"                        }\n                    }\n                    layer.Update(element);\n                }\n            }\n        }\n\n        /// <summary>\n        /// Verify that no other annotation services are attached above, on or below root\n        /// </summary>\n        /// <param name=\"root\">the proposed root for a new AnnotationService being enabled</param>\n        /// <exception cref=\"InvalidOperationException\">Other Instance of AnnotationService Is Already Set</exception>\n        private static void VerifyServiceConfiguration(DependencyObject root)\n        {\n            Invariant.Assert(root != null, \"Parameter 'root' is null.\");\n\n            // First make sure there isn't a service above us\n            if (AnnotationService.GetService(root) != null)\n                throw new InvalidOperationException(SR.AnnotationServiceAlreadyExists);\n\n            // Now check the tree below us for an existing service\n            DescendentsWalker<Object> walker = new DescendentsWalker<Object>(TreeWalkPriority.VisualTree, VerifyNoServiceOnNode, null);\n            walker.StartWalk(root);\n        }\n\n        /// <summary>\n        /// Finds the DocumentViewerBase object if root is DocumentViewerBase or FlowDocumentReader\n        /// in paginated mode. If the root is FDSV or FDR in scroll mode - sets only the document;\n        /// </summary>\n        /// <param name=\"root\">root the service is enabled on</param>\n        /// <param name=\"documentViewerBase\">DocumentViewerBase used by the viewer</param>\n        /// <param name=\"document\">document for the viewer</param>\n        private static void GetViewerAndDocument(DependencyObject root, out DocumentViewerBase documentViewerBase, out IDocumentPaginatorSource document)\n        {\n            documentViewerBase = root as DocumentViewerBase;\n\n            // Initialize out parameters","sourceCodeStart":1019,"sourceCodeEnd":1055,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationService.cs#L1019-L1055","documentation":"During Enable, VerifyServiceConfiguration checks that no other AnnotationService is attached at or above the given root via the ServiceProperty, and that none exists in the visual tree below. Finding one throws this InvalidOperationException because only one annotation service may govern a subtree.","triggerScenarios":"Calling Enable() on a second AnnotationService whose root already has a service attached (its own or an ancestor's), or where any descendant element in the visual tree carries an AnnotationService.","commonSituations":"Nested viewers each with their own AnnotationService; re-enabling a new service on a viewer whose old service was never disabled; a parent FlowDocumentPageViewer already annotated when a child control's service is enabled.","solutions":["Disable the existing service (AnnotationService.GetService(root)?.Disable()) before enabling a new one.","Reuse the existing service instance instead of constructing a new one.","Restructure so only one service covers the subtree; annotate sibling subtrees independently."],"exampleFix":"// before\nvar s2 = new AnnotationService(viewer); s2.Enable(store); // service exists\n// after\nvar existing = AnnotationService.GetService(viewer);\nvar service = existing ?? new AnnotationService(viewer);\nif (existing == null) service.Enable(store);","handlingStrategy":"type-guard","validationCode":"if (AnnotationService.GetService(root) != null) return; // a service already exists","typeGuard":"bool IsFree(DependencyObject root) => AnnotationService.GetService(root) == null;","tryCatchPattern":"try { service.Enable(store); } catch (InvalidOperationException) { /* reuse the existing service instead */ }","preventionTips":["Cache and reuse one AnnotationService per viewer","Always Disable before creating a new service on the same subtree"],"tags":["wpf","annotations","duplicate-instance","invalid-state"],"backgroundTag":"conflicting-config-options","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}