dotnet/wpf · error · ArgumentException
SR.AnnotationServiceNotEnabled
Error message
SR.AnnotationServiceNotEnabled
What it means
AnnotationHelper.CheckInputs throws ArgumentException when the AnnotationService passed to a helper method (CreateHighlightForSelection, CreateTextStickyNoteForSelection, etc.) has not been enabled. The helper methods require an actively enabled service because they operate on the service's viewer and store. Calling a helper with a service that Enable() was never called on (or after Disable()) produces this error.
Solutions
- Call service.Enable(annotationStore) on the AnnotationService before invoking any AnnotationHelper method.
- Check service.IsEnabled prior to calling the helper and enable it if false.
- Ensure you are passing the same AnnotationService instance that was enabled for the viewer, not a newly constructed one.
- If the service was disabled (e.g. on page unload), re-enable it before further helper calls.
Example fix
// before
var service = new AnnotationService(fdvViewer);
AnnotationHelper.CreateHighlightForSelection(service, "me", "note", anchors, rects);
// after
var service = new AnnotationService(fdvViewer);
service.Enable(new XmlStreamStore(new Uri("annotations.xml")));
if (service.IsEnabled)
AnnotationHelper.CreateHighlightForSelection(service, "me", "note", anchors, rects); Defensive patterns
Strategy: validation
Validate before calling
if (service == null) throw new ArgumentNullException(nameof(service));
if (!service.IsEnabled)
service.Enable(store); // enable before helper calls Type guard
bool CanUseHelpers(AnnotationService s) => s != null && s.IsEnabled;
Try / catch
try { AnnotationHelper.CreateHighlightForSelection(service, ...); }
catch (ArgumentException ex) when (ex.Message.Contains(nameof(service))) { /* enable service and retry */ } Prevention
- Always Enable the service immediately after constructing it and before any helper use.
- Centralize helper calls behind a helper that checks IsEnabled.
- Disable/enable the service symmetrically in navigation lifecycle events.
When it happens
Trigger: Calling any public AnnotationHelper static method (e.g. AnnotationHelper.CreateHighlightForSelection(service, ...)) with an AnnotationService instance whose IsEnabled property is false, i.e. Enable() was never called or Disable() was called beforehand.
Common situations: Creating the AnnotationService and immediately calling AnnotationHelper without first calling service.Enable(store); calling helpers after service.Disable(); enabling the service asynchronously and calling a helper before Enable completes; using a different service instance than the one enabled for the viewer.
Related errors
- SR.AnnotationServiceAlreadyExists
- SR.AnnotationServiceIsAlreadyEnabled
- SR.Format(SR.ComponentAlreadyInPresentationContext…
- SR.Format(SR.ComponentNotInPresentationContext, component)
- SR.Format(SR.IncorrectLocatorPartType, " : ")
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/f1d7e43a0d6e1b43.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/AnnotationHelper.cs:1084
}
annot.Anchors.Clear();
annot.Anchors.Add(anchor);
}
/// <summary>
/// Common input checks. Service must be non-null and enabled.
/// </summary>
/// <param name="service">service to check</param>
/// <exception cref="ArgumentNullException">service is null</exception>
/// <exception cref="ArgumentException">service is not enabled</exception>
private static void CheckInputs(AnnotationService service)
{
ArgumentNullException.ThrowIfNull(service);
if (!service.IsEnabled)
{
throw new ArgumentException(SR.AnnotationServiceNotEnabled, nameof(service));
}
DocumentViewerBase viewer = service.Root as DocumentViewerBase;
if (viewer == null)
{
FlowDocumentScrollViewer scrollViewer = service.Root as FlowDocumentScrollViewer;
FlowDocumentReader reader = service.Root as FlowDocumentReader;
Invariant.Assert((scrollViewer != null) || (reader != null), "Service's Root must be either a FlowDocumentReader, DocumentViewerBase or a FlowDocumentScrollViewer.");
}
else
{
if (viewer.Document == null)
{
throw new InvalidOperationException(SR.OnlyFlowFixedSupported);
}
}
}
View on GitHub (pinned to 81131a70a4)