dotnet/wpf · error · ArgumentNullException

serviceType

Error message

serviceType

What it means

TextFlow's explicit IServiceProvider.GetService implementation throws ArgumentNullException when serviceType is null. The contract requires a concrete service Type; null is not a queryable service identity.

Solutions

  1. Pass a valid Type such as typeof(ITextContainer) or typeof(TextContainer)
  2. Guard the serviceType argument for null before calling GetService
  3. Fix the upstream code that produced a null Type (failed typeof/Type.GetType call)

Example fix

// before
var svc = textFlow.GetService(null);

// after
if (serviceType != null)
    var svc = textFlow.GetService(serviceType);
Defensive patterns

Strategy: validation

Validate before calling

if (serviceType == null) throw new ArgumentNullException(nameof(serviceType));
var svc = textFlow.GetService(serviceType);

Type guard

bool isValidServiceType(Type t) => t != null;

Try / catch

try { svc = textFlow.GetService(serviceType); } catch (ArgumentNullException) { /* null type passed; fix caller */ }

Prevention

When it happens

Trigger: Calling GetService(null) on a TextFlow instance obtained via IServiceProvider (e.g., from a TextEditor/TextContainer service provider context).

Common situations: Generic service-locator helper code that forwards an unresolved Type variable; reflection-based code where the requested service type failed to resolve to a Type.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/25f9b37b9ad72808. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextFlow.cs:191

        /// <summary>
        /// Gets the service object of the specified type.
        /// </summary>
        /// <remarks>
        /// TextFlow currently supports only TextView and TextContainer.
        /// </remarks>
        /// <param name="serviceType">
        /// An object that specifies the type of service object to get.
        /// </param>
        /// <returns>
        /// A service object of type serviceType. A null reference if there is no 
        /// service object of type serviceType.
        /// </returns>
        object IServiceProvider.GetService(Type serviceType)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException("serviceType");
            }
            if (serviceType == typeof(ITextContainer))
            {
                return _structuralCache.TextContainer;
            }
            else if (serviceType == typeof(TextContainer))
            {
                return _structuralCache.TextContainer as TextContainer;
            }
            else if (serviceType == typeof(ITextView))
            {
                return this.TextView;
            }
            return null;
        }

        #endregion IServiceProvider Members

View on GitHub (pinned to 81131a70a4)