{"record":{"id":"1d611d717502ba50","repo":"dotnet/wpf","slug":"sr-markupextensionnocontext","errorCode":null,"errorMessage":"SR.MarkupExtensionNoContext","messagePattern":"SR\\.MarkupExtensionNoContext","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ColorConvertedBitmapExtension.cs","lineNumber":92,"sourceCode":"        {\n            if (_image == null)\n            {\n                throw new InvalidOperationException(SR.ColorConvertedBitmapExtensionNoSourceImage);\n            }\n            if (_sourceProfile == null)\n            {\n                throw new InvalidOperationException(SR.ColorConvertedBitmapExtensionNoSourceProfile);\n            }\n            \n            // [BreakingChange] \n            // (NullReferenceException in ColorConvertedBitmapExtension.ProvideValue)\n            // We really should throw an ArgumentNullException here for serviceProvider.\n\n            // Save away the BaseUri.\n            IUriContext uriContext = serviceProvider.GetService(typeof(IUriContext)) as IUriContext;\n            if( uriContext == null )\n            {\n                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, \"IUriContext\" ));\n            }\n            _baseUri = uriContext.BaseUri;\n            \n\n            Uri imageUri = GetResolvedUri(_image);\n            Uri sourceProfileUri = GetResolvedUri(_sourceProfile);\n            Uri destinationProfileUri = GetResolvedUri(_destinationProfile);\n\n            ColorContext sourceContext = new ColorContext(sourceProfileUri);\n            ColorContext destinationContext = destinationProfileUri != null ?\n                                                new ColorContext(destinationProfileUri) :\n                                                new ColorContext(PixelFormats.Default);\n\n            BitmapDecoder decoder = BitmapDecoder.Create(\n                                                                imageUri,\n                                                                BitmapCreateOptions.IgnoreColorProfile | BitmapCreateOptions.IgnoreImageCache,\n                                                                BitmapCacheOption.None\n                                                                );","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ColorConvertedBitmapExtension.cs#L74-L110","documentation":"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.","triggerScenarios":"Calling ProvideValue with a null IServiceProvider, or from a context that does not supply IUriContext (e.g. invoking the extension outside normal XAML parsing).","commonSituations":"Unit tests calling ProvideValue(new ServiceObject()) with no IUriContext registered; using the extension programmatically without a XAML parser-provided service provider.","solutions":["Only evaluate the extension through the normal XAML parser/BAML loader, which supplies IUriContext","In custom code, pass a service provider that implements or provides IUriContext (e.g. via a ServiceProviderValueSerializer context)","In tests, implement a stub IServiceProvider returning an IUriContext with a BaseUri"],"exampleFix":"// before\nobject value = extension.ProvideValue(null); // throws\n// after\nvar provider = new TestServiceProvider(); // returns IUriContext with BaseUri set\nobject value = extension.ProvideValue(provider);","handlingStrategy":"try-catch","validationCode":"if (serviceProvider == null || serviceProvider.GetService(typeof(IUriContext)) == null)\n    throw new ArgumentException(\"Service provider must supply IUriContext.\");","typeGuard":"static bool HasUriContext(IServiceProvider sp) => sp?.GetService(typeof(IUriContext)) is IUriContext;","tryCatchPattern":"try { var bmp = (BitmapSource)ext.ProvideValue(serviceProvider); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"IUriContext\")) { /* use a provider that supplies IUriContext */ }","preventionTips":["Let the XAML parser invoke the extension rather than calling ProvideValue manually","Provide a stub IServiceProvider with IUriContext in unit tests","Set BaseUri correctly when hosting extension evaluation in custom parsers"],"tags":["wpf","xaml","markup-extension","service-provider"],"backgroundTag":"missing-dependency","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}