dotnet/wpf · error · InvalidOperationException
SR.MustNotCallSetter
Error message
SR.MustNotCallSetter
What it means
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.
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.
Example fix
// before
var uriContext = (IUriContext)serviceProvider.GetService(typeof(IUriContext));
uriContext.BaseUri = new Uri("pack://application:,,,/MyAssembly/");
var full = uriContext.BaseUri;
// after
var uriContext = (IUriContext)serviceProvider.GetService(typeof(IUriContext));
var resolved = uriContext.BaseUri != null
? new Uri(uriContext.BaseUri, relativeUri)
: relativeUri; Defensive patterns
Strategy: try-catch
Validate before calling
var uriContext = serviceProvider.GetService(typeof(IUriContext)) as IUriContext;
if (uriContext == null) throw new InvalidOperationException("No IUriContext available"); Type guard
bool CanSetBaseUri(object sp) => sp is not IUriContext u || u.GetType().GetProperty(nameof(IUriContext.BaseUri))?.CanWrite == true;
Try / catch
try { uriContext.BaseUri = newUri; }
catch (InvalidOperationException) { // read-only view: combine URIs instead
resolved = uriContext.BaseUri != null ? new Uri(uriContext.BaseUri, relative) : relative; } Prevention
- 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
When it happens
Trigger: 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(...).
Common situations: 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.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- SR.MustNotCallSetter
- Animation_Invalid_DefaultValue
- Cannot remove signature from read-only file.
- Image_EncoderNoColorContext
- Image_EncoderNoGlobalMetadata
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/68047e910a7c8341.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Context/ServiceProviderContext.cs:123
PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor
{
get { return null; }
}
#endregion
#region IXamlTypeResolver Members
Type IXamlTypeResolver.Resolve(string qName)
{
return _xamlContext.ServiceProvider_Resolve(qName);
}
#endregion
#region IUriContext Members
Uri IUriContext.BaseUri
{
get { return _xamlContext.BaseUri; }
set { throw new InvalidOperationException(SR.MustNotCallSetter); }
}
#endregion
#region IAmbientProvider Members
AmbientPropertyValue IAmbientProvider.GetFirstAmbientValue(
IEnumerable<XamlType> ceilingTypes,
params XamlMember[] properties)
{
ArgumentNullException.ThrowIfNull(properties);
foreach (var property in properties)
{
if (property is null)
{
// we don't allow any property to be null
throw new ArgumentException(SR.Format(SR.ValueInArrayIsNull, "properties"));
}
}View on GitHub (pinned to 81131a70a4)