{"record":{"id":"b6f70e70ccff799d","repo":"dotnet/wpf","slug":"sr-selectionmustbeserviceprovider","errorCode":null,"errorMessage":"SR.SelectionMustBeServiceProvider","messagePattern":"SR\\.SelectionMustBeServiceProvider","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextViewSelectionProcessor.cs","lineNumber":264,"sourceCode":"        //\n        //  Private Methods\n        //\n        //------------------------------------------------------        \n\n        #region Private Methods        \n\n        /// <summary>\n        ///   Verify the selection is of the correct type.\n        /// </summary>\n        /// <param name=\"selection\">selection to verify</param>\n        /// <returns>the selection cast to the necessary type</returns>\n        private ITextView VerifySelection(object selection)\n        {\n            ArgumentNullException.ThrowIfNull(selection);\n\n            IServiceProvider provider = selection as IServiceProvider;\n            if (provider == null)\n                throw new ArgumentException(SR.SelectionMustBeServiceProvider, nameof(selection));\n\n            ITextView textView = provider.GetService(typeof(ITextView)) as ITextView;\n\n            return textView;\n        }\n\n        #endregion Private Methods\n\n        //------------------------------------------------------\n        //\n        //  Private Fields\n        //\n        //------------------------------------------------------\n\n        #region Private Fields\n\n        // ContentLocator part types understood by this processor\n        private static readonly XmlQualifiedName[] LocatorPartTypeNames = Array.Empty<XmlQualifiedName>();","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/TextViewSelectionProcessor.cs#L246-L282","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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)"],"exampleFix":"// before\nprocessor.GetAnchorPoint(someTextRange);\n// after\nITextView tv = myTextView; // IServiceProvider\nprocessor.GetAnchorPoint(tv);","handlingStrategy":"type-guard","validationCode":"if (selection is IServiceProvider p && p.GetService(typeof(ITextView)) is ITextView)\n{ /* safe to call */ }","typeGuard":"bool IsTextViewSelection(object selection) =>\n    selection is IServiceProvider p && p.GetService(typeof(ITextView)) is ITextView;","tryCatchPattern":"try { var nodes = processor.GetSelectedNodes(selection); }\ncatch (ArgumentException ex) when (ex.Message == SR.SelectionMustBeServiceProvider) { /* wrong processor for this selection */ }","preventionTips":["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"],"tags":["wpf","annotations","type-mismatch","textview"],"backgroundTag":"type-mismatch","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"}