{"record":{"id":"9a1e4241ee430ae5","repo":"dotnet/wpf","slug":"sr-format-sr-markupextensionnocontext-gettype-name-9a1e42","errorCode":null,"errorMessage":"SR.Format(SR.MarkupExtensionNoContext, GetType().Name, \"IProvideValueTarget\")","messagePattern":"SR\\.Format\\(SR\\.MarkupExtensionNoContext, GetType\\(\\)\\.Name, \"IProvideValueTarget\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeDictionaryExtension.cs","lineNumber":74,"sourceCode":"        ///  Return an object that should be set on the targetObject's targetProperty\n        ///  for this markup extension.  For ThemeDictionaryExtension, this is the Uri\n        ///  pointing to theme specific dictionary in the specified assembly by AssemblyName.\n        /// </summary>\n        /// <param name=\"serviceProvider\">ServiceProvider that can be queried for services.</param>\n        /// <returns>\n        ///  The object to set on this property.\n        /// </returns>\n        public override object ProvideValue(IServiceProvider serviceProvider)\n        {\n            if (string.IsNullOrEmpty(AssemblyName))\n            {\n                throw new InvalidOperationException(SR.ThemeDictionaryExtension_Name);\n            }\n\n            IProvideValueTarget provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;\n            if( provideValueTarget == null )\n            {\n                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, \"IProvideValueTarget\" ));\n            }\n                \n            object targetObject = provideValueTarget.TargetObject;\n            object targetProperty = provideValueTarget.TargetProperty;\n\n            ResourceDictionary dictionary = targetObject as ResourceDictionary;\n            PropertyInfo propertyInfo = targetProperty as PropertyInfo;\n\n            // Allow targetProperty to be null or ResourceDictionary.Source\n            if (dictionary == null || (targetProperty != null && propertyInfo != SourceProperty))\n            {\n                throw new InvalidOperationException(SR.ThemeDictionaryExtension_Source);\n            }\n\n            Register(dictionary, _assemblyName);\n            dictionary.IsSourcedFromThemeDictionary = true;\n\n            return GenerateUri(_assemblyName, SystemResources.ResourceDictionaries.ThemedResourceName, MS.Win32.UxThemeWrapper.ThemeName);","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThemeDictionaryExtension.cs#L56-L92","documentation":"ProvideValue needs the WPF XAML parser's IProvideValueTarget service to learn which object/property the extension is being applied to. When the service provider returns null (extension used outside a XAML parse, e.g. in design-time tools or manually constructed markup-extension evaluation), it throws InvalidOperationException naming MarkupExtensionNoContext.","triggerScenarios":"Calling ThemeDictionaryExtension.ProvideValue with an IServiceProvider that does not implement IProvideValueTarget; evaluating the extension outside of XAML loading (unit tests, code-generated dictionaries).","commonSituations":"Unit tests that build ResourceDictionaries in code and evaluate markup extensions manually; custom XAML loaders missing the standard service provider; designer/host environments that partially implement the service provider.","solutions":["Only evaluate the extension through the WPF XAML loader, which supplies IProvideValueTarget.","In custom hosts, provide a service provider implementing IProvideValueTarget that returns the target ResourceDictionary and its Source property.","Construct the theme-sourced dictionary directly in code (set ResourceDictionary.Source to a theme-pack URI) instead of using the extension."],"exampleFix":"// before\nvar sp = new ServiceProvider(); // no IProvideValueTarget\nvar uri = (Uri)ext.ProvideValue(sp); // throws\n// after\nvar rd = new ResourceDictionary\n{\n    Source = new Uri(\"pack://application:,,,/MyApp;component/Themes/Default.xaml\")\n};","handlingStrategy":"try-catch","validationCode":"var target = serviceProvider?.GetService(typeof(IProvideValueTarget));\nif (target == null) throw new InvalidOperationException(\"ProvideValue requires IProvideValueTarget from a XAML host.\");","typeGuard":"bool HasContext(IServiceProvider sp) => sp?.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget;","tryCatchPattern":"try { return (Uri)ext.ProvideValue(sp); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"IProvideValueTarget\")) { return FallbackUri(); }","preventionTips":["Evaluate markup extensions only via the XAML loader.","In custom hosts, implement and register IProvideValueTarget.","Prefer setting ResourceDictionary.Source directly in code."],"tags":["wpf","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"}