dotnet/wpf · error · InvalidOperationException

SR.MarkupExtensionNoContext

Error message

SR.MarkupExtensionNoContext

What it means

The extension needs the service provider to expose IUriContext so it can resolve relative image/profile URIs against a base URI. If serviceProvider is null or GetService(typeof(IUriContext)) returns null, ProvideValue throws InvalidOperationException (MarkupExtensionNoContext) naming the extension and the missing service.

Solutions

  1. Only evaluate the extension through the normal XAML parser/BAML loader, which supplies IUriContext
  2. In custom code, pass a service provider that implements or provides IUriContext (e.g. via a ServiceProviderValueSerializer context)
  3. In tests, implement a stub IServiceProvider returning an IUriContext with a BaseUri

Example fix

// before
object value = extension.ProvideValue(null); // throws
// after
var provider = new TestServiceProvider(); // returns IUriContext with BaseUri set
object value = extension.ProvideValue(provider);
Defensive patterns

Strategy: try-catch

Validate before calling

if (serviceProvider == null || serviceProvider.GetService(typeof(IUriContext)) == null)
    throw new ArgumentException("Service provider must supply IUriContext.");

Type guard

static bool HasUriContext(IServiceProvider sp) => sp?.GetService(typeof(IUriContext)) is IUriContext;

Try / catch

try { var bmp = (BitmapSource)ext.ProvideValue(serviceProvider); }
catch (InvalidOperationException ex) when (ex.Message.Contains("IUriContext")) { /* use a provider that supplies IUriContext */ }

Prevention

When it happens

Trigger: Calling ProvideValue with a null IServiceProvider, or from a context that does not supply IUriContext (e.g. invoking the extension outside normal XAML parsing).

Common situations: Unit tests calling ProvideValue(new ServiceObject()) with no IUriContext registered; using the extension programmatically without a XAML parser-provided service provider.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ColorConvertedBitmapExtension.cs:92

        {
            if (_image == null)
            {
                throw new InvalidOperationException(SR.ColorConvertedBitmapExtensionNoSourceImage);
            }
            if (_sourceProfile == null)
            {
                throw new InvalidOperationException(SR.ColorConvertedBitmapExtensionNoSourceProfile);
            }
            
            // [BreakingChange] 
            // (NullReferenceException in ColorConvertedBitmapExtension.ProvideValue)
            // We really should throw an ArgumentNullException here for serviceProvider.

            // Save away the BaseUri.
            IUriContext uriContext = serviceProvider.GetService(typeof(IUriContext)) as IUriContext;
            if( uriContext == null )
            {
                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, "IUriContext" ));
            }
            _baseUri = uriContext.BaseUri;
            

            Uri imageUri = GetResolvedUri(_image);
            Uri sourceProfileUri = GetResolvedUri(_sourceProfile);
            Uri destinationProfileUri = GetResolvedUri(_destinationProfile);

            ColorContext sourceContext = new ColorContext(sourceProfileUri);
            ColorContext destinationContext = destinationProfileUri != null ?
                                                new ColorContext(destinationProfileUri) :
                                                new ColorContext(PixelFormats.Default);

            BitmapDecoder decoder = BitmapDecoder.Create(
                                                                imageUri,
                                                                BitmapCreateOptions.IgnoreColorProfile | BitmapCreateOptions.IgnoreImageCache,
                                                                BitmapCacheOption.None
                                                                );

View on GitHub (pinned to 81131a70a4)