{"record":{"id":"a94d089e28382f27","repo":"dotnet/wpf","slug":"sr-annotationserviceisalreadyenabled","errorCode":null,"errorMessage":"SR.AnnotationServiceIsAlreadyEnabled","messagePattern":"SR\\.AnnotationServiceIsAlreadyEnabled","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationService.cs","lineNumber":165,"sourceCode":"        #region Public Methods\n\n        /// <summary>\n        ///     Enables the service with the given store.\n        /// </summary>\n        /// <param name=\"annotationStore\">store to use for retreiving and persisting annotations</param>\n        /// <exception cref=\"ArgumentNullException\">store is null</exception>\n        /// <exception cref=\"InvalidOperationException\">DocumentViewerBase has content which is neither\n        /// FlowDocument or FixedDocument; only those two are supported</exception>\n        /// <exception cref=\"InvalidOperationException\">this service or another service is already\n        /// enabled for the DocumentViewerBase</exception>\n        public void Enable(AnnotationStore annotationStore)\n        {\n            ArgumentNullException.ThrowIfNull(annotationStore);\n\n            VerifyAccess();\n\n            if (_isEnabled)\n                throw new InvalidOperationException(SR.AnnotationServiceIsAlreadyEnabled);\n\n            // Make sure there isn't a service above or below us\n            VerifyServiceConfiguration(_root);\n\n            // Post a background work item to load existing annotations.  Do this early in the\n            // Enable method in case any later code causes an indirect call to LoadAnnotations,\n            // this cached operation will make that LoadAnnotations call a no-op.\n            _asyncLoadOperation = _root.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(LoadAnnotationsAsync), this);\n\n            // Enable the store and set it on the tree\n            _isEnabled = true;\n            _root.SetValue(AnnotationService.ServiceProperty, this);\n            _store = annotationStore;\n\n            // If our root is a DocumentViewerBase we need to setup some special processors.\n            DocumentViewerBase viewer = _root as DocumentViewerBase;\n            if (viewer != null)\n            {","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationService.cs#L147-L183","documentation":"AnnotationService.Enable throws InvalidOperationException when Enable is called on a service instance that is already enabled (_isEnabled is true). Each AnnotationService can be enabled exactly once until Disable is called; re-enabling the live instance is an invalid state transition. This follows VerifyAccess, so it only occurs on the dispatcher thread.","triggerScenarios":"Calling service.Enable(store) twice on the same AnnotationService instance without an intervening Disable(), e.g. Enable invoked from both a Loaded event and a navigation handler, or re-Enable after the viewer re-loads while the service stayed enabled.","commonSituations":"Double initialization from overlapping UI events (Loaded firing multiple times, tab re-activation); application startup code running twice; forgetting that Enable on a new store requires Disable first; MVVM re-attach logic enabling an already-enabled service.","solutions":["Check service.IsEnabled before calling Enable and skip if already true.","Call service.Disable() before re-enabling with a different store.","Guard initialization with a flag so overlapping Loaded/navigation events cannot enable twice.","Create a fresh AnnotationService instance (disabling the old one first) if a new store is required."],"exampleFix":"// before\nservice.Enable(store); // may run again on re-Load\n\n// after\nif (!service.IsEnabled)\n    service.Enable(store);","handlingStrategy":"validation","validationCode":"if (!service.IsEnabled)\n    service.Enable(store);","typeGuard":"bool CanEnable(AnnotationService s) => s != null && !s.IsEnabled;","tryCatchPattern":"try { service.Enable(store); }\ncatch (InvalidOperationException) { /* already enabled — no-op */ }","preventionTips":["Guard all Enable calls with an IsEnabled check.","Initialize the service once in a single lifecycle hook, not in Loaded and navigation handlers both.","Always pair Enable with Disable in teardown to allow clean re-enabling.","Use an idempotent EnsureEnabled() wrapper around Enable."],"tags":["wpf","annotations","invalid-state","double-initialization"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}