dotnet/wpf · error · ArgumentException
SR.SelectionMustBeServiceProvider
Error message
SR.SelectionMustBeServiceProvider
What it means
TextViewSelectionProcessor.VerifySelection throws ArgumentException(SR.SelectionMustBeServiceProvider) when the selection object passed to a TextView-based selection processor is not an IServiceProvider. TextView selections must expose GetService to yield the underlying ITextView used for anchoring.
Solutions
- Pass the ITextView (which is an IServiceProvider) or the document's TextSelection/TextPointer-derived selection the processor expects
- Check selection is IServiceProvider and GetService(typeof(ITextView)) returns non-null before calling
- Use the matching selection processor for your host (TextSelectionProcessor for text, TreeNodeSelectionProcessor for trees)
Example fix
// before processor.GetAnchorPoint(someTextRange); // after ITextView tv = myTextView; // IServiceProvider processor.GetAnchorPoint(tv);
Defensive patterns
Strategy: type-guard
Validate before calling
if (selection is IServiceProvider p && p.GetService(typeof(ITextView)) is ITextView)
{ /* safe to call */ } Type guard
bool IsTextViewSelection(object selection) =>
selection is IServiceProvider p && p.GetService(typeof(ITextView)) is ITextView; Try / catch
try { var nodes = processor.GetSelectedNodes(selection); }
catch (ArgumentException ex) when (ex.Message == SR.SelectionMustBeServiceProvider) { /* wrong processor for this selection */ } Prevention
- Match the selection processor to the host (text vs tree)
- Verify the selection exposes ITextView via GetService before anchoring
- Don't pass raw TextRange/data objects to TextView processors
When it happens
Trigger: Calling GetSelectedNodes/GetParent/GetAnchorPoint on a TextViewSelectionProcessor with a selection object that doesn't implement IServiceProvider (e.g. a raw string, TextRange, or arbitrary object).
Common situations: Mixing selection processors — passing a tree-node UIElement to a text selection processor; custom annotation clients storing non-service-provider selections; refactors changing the selection type.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- SR.AnnotationAdorner_NotUIElement
- SR.Format(SR.IncorrectLocatorPartType, " : ")
- SR.IncorrectLocatorPartType
- SR.InvalidAttachedAnchor
- SR.InvalidAttachedAnchor
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b6f70e70ccff799d.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextViewSelectionProcessor.cs:264
//
// Private Methods
//
//------------------------------------------------------
#region Private Methods
/// <summary>
/// Verify the selection is of the correct type.
/// </summary>
/// <param name="selection">selection to verify</param>
/// <returns>the selection cast to the necessary type</returns>
private ITextView VerifySelection(object selection)
{
ArgumentNullException.ThrowIfNull(selection);
IServiceProvider provider = selection as IServiceProvider;
if (provider == null)
throw new ArgumentException(SR.SelectionMustBeServiceProvider, nameof(selection));
ITextView textView = provider.GetService(typeof(ITextView)) as ITextView;
return textView;
}
#endregion Private Methods
//------------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
#region Private Fields
// ContentLocator part types understood by this processor
private static readonly XmlQualifiedName[] LocatorPartTypeNames = Array.Empty<XmlQualifiedName>();View on GitHub (pinned to 81131a70a4)