{"record":{"id":"68047e910a7c8341","repo":"dotnet/wpf","slug":"sr-mustnotcallsetter","errorCode":null,"errorMessage":"SR.MustNotCallSetter","messagePattern":"SR\\.MustNotCallSetter","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ServiceProviderContext.cs","lineNumber":123,"sourceCode":"\n        PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor\n        {\n            get { return null; }\n        }\n        #endregion\n\n        #region IXamlTypeResolver Members\n        Type IXamlTypeResolver.Resolve(string qName)\n        {\n            return _xamlContext.ServiceProvider_Resolve(qName);\n        }\n        #endregion\n\n        #region IUriContext Members\n        Uri IUriContext.BaseUri\n        {\n            get { return _xamlContext.BaseUri; }\n            set { throw new InvalidOperationException(SR.MustNotCallSetter); }\n        }\n        #endregion\n\n        #region IAmbientProvider Members\n        AmbientPropertyValue IAmbientProvider.GetFirstAmbientValue(\n                                                    IEnumerable<XamlType> ceilingTypes,\n                                                    params XamlMember[] properties)\n        {\n            ArgumentNullException.ThrowIfNull(properties);\n\n            foreach (var property in properties)\n            {\n                if (property is null)\n                {\n                    // we don't allow any property to be null\n                    throw new ArgumentException(SR.Format(SR.ValueInArrayIsNull, \"properties\"));\n                }\n            }","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ServiceProviderContext.cs#L105-L141","documentation":"ServiceProviderContext exposes IUriContext.BaseUri with a get-only implementation; calling the setter throws InvalidOperationException. The context intentionally forbids changing the base URI through this service provider view — BaseUri must be set on the underlying XamlParser/AmbientProvider context, not via the service provider handed to markup extensions and type converters.","triggerScenarios":"Code obtains an IUriContext from a XAML service provider (IXamlSchemaContextProvider/IServiceProvider.GetService(typeof(IUriContext))) and assigns to its BaseUri property, e.g. ((IUriContext)serviceProvider).BaseUri = new Uri(...).","commonSituations":"Markup extensions (MarkupExtension/TypeConverter ProvideValue) trying to rewrite the base URI to resolve relative URIs; ported code that previously ran in a framework where IUriContext.BaseUri was settable; generic library code that sets BaseUri defensively before reading it.","solutions":["Read BaseUri and combine it with the relative path via new Uri(baseUri, relativeUri) instead of assigning to it.","Set the base URI where it is owned: pass it to XamlXmlReader/XamlXmlReaderSettings.BaseUri (or the parser setup) rather than the service provider.","If runtime adjustment is required, implement your own IUriContext wrapper or supply the resolved URI to your markup extension as a property.","Catch InvalidOperationException around the setter only as a last resort; treat it as a contract violation."],"exampleFix":"// before\nvar uriContext = (IUriContext)serviceProvider.GetService(typeof(IUriContext));\nuriContext.BaseUri = new Uri(\"pack://application:,,,/MyAssembly/\");\nvar full = uriContext.BaseUri;\n// after\nvar uriContext = (IUriContext)serviceProvider.GetService(typeof(IUriContext));\nvar resolved = uriContext.BaseUri != null\n    ? new Uri(uriContext.BaseUri, relativeUri)\n    : relativeUri;","handlingStrategy":"try-catch","validationCode":"var uriContext = serviceProvider.GetService(typeof(IUriContext)) as IUriContext;\nif (uriContext == null) throw new InvalidOperationException(\"No IUriContext available\");","typeGuard":"bool CanSetBaseUri(object sp) => sp is not IUriContext u || u.GetType().GetProperty(nameof(IUriContext.BaseUri))?.CanWrite == true;","tryCatchPattern":"try { uriContext.BaseUri = newUri; }\ncatch (InvalidOperationException) { // read-only view: combine URIs instead\n  resolved = uriContext.BaseUri != null ? new Uri(uriContext.BaseUri, relative) : relative; }","preventionTips":["Treat IUriContext from a service provider as read-only","Configure BaseUri via XamlXmlReaderSettings at parse start","Use new Uri(base, relative) composition instead of assignment"],"tags":["wpf","system-xaml","invalid-operation","uri-context","setter-forbidden"],"backgroundTag":"unsupported-operation","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"}