{"record":{"id":"29b08debf02579a1","repo":"dotnet/wpf","slug":"sr-annotationservicenotenabled-annotationservice","errorCode":null,"errorMessage":"SR.AnnotationServiceNotEnabled","messagePattern":"SR\\.AnnotationServiceNotEnabled","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationService.cs","lineNumber":225,"sourceCode":"\n            // Don't register on the store until the service is ready to accept events.\n            // Register for content change and anchor change events - we'll need to attach/detach\n            // annotations as they are added/removed from the store or their anchors change\n            annotationStore.StoreContentChanged += new StoreContentChangedEventHandler(OnStoreContentChanged);\n            annotationStore.AnchorChanged += new AnnotationResourceChangedEventHandler(OnAnchorChanged);\n        }\n\n        /// <summary>\n        ///     Disables annotations on the DocumentViewerBase.  Any visible annotations will\n        ///     disappear.  Service can be enabled again by calling Enable.\n        /// </summary>\n        /// <exception cref=\"InvalidOperationException\">service isn't enabled</exception>\n        public void Disable()\n        {\n            VerifyAccess();\n\n            if (!_isEnabled)\n                throw new InvalidOperationException(SR.AnnotationServiceNotEnabled);\n\n            // If it hasn't been run yet, abort the pending operation.  Still need to\n            // unregister and unload annotations.  They may have been loaded due to a\n            // store event.\n            _asyncLoadOperation?.Abort();\n            _asyncLoadOperation = null;\n\n            // Unregister for changes to the store - add/deletes/anchor changes - before\n            // unloading annotations.  We don't want any events between unloading and unregistering.\n            try\n            {\n                _store.StoreContentChanged -= new StoreContentChangedEventHandler(OnStoreContentChanged);\n                _store.AnchorChanged -= new AnnotationResourceChangedEventHandler(OnAnchorChanged);\n            }\n            finally\n            {\n                IDocumentPaginatorSource document;\n                DocumentViewerBase viewer;","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationService.cs#L207-L243","documentation":"AnnotationService.Disable throws this InvalidOperationException when the service has not been enabled (the internal _isEnabled flag is false). Disable is only valid after a successful Enable call on the same service instance.","triggerScenarios":"Calling Disable() on a service that was never enabled, or calling Disable() twice (double-teardown, e.g. in both a page unload handler and a Dispose method).","commonSituations":"See trigger scenarios.","solutions":["Guard the Disable call with AnnotationService.GetService(viewer) != null or a tracked bool before calling it.","Structure teardown so Disable is called exactly once per Enable (e.g. set the service field to null after disabling)."],"exampleFix":"// before\nservice.Disable();\n// after\nif (AnnotationService.GetService(myViewer) is AnnotationService service)\n    service.Disable();","handlingStrategy":"type-guard","validationCode":"bool isEnabled = AnnotationService.GetService(myViewer) != null;","typeGuard":"bool CanDisable(AnnotationService s) => AnnotationService.GetService(s != null ? GetViewer(s) : null) != null;","tryCatchPattern":"try { service.Disable(); } catch (InvalidOperationException) { /* never enabled; ignore */ }","preventionTips":["Pair every Enable with exactly one Disable","Track service lifecycle state in your own field","Make teardown idempotent"],"tags":["wpf","annotations","invalid-state-transition","lifecycle"],"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"}