dotnet/wpf · error · InvalidOperationException
SR.MustHaveName
Error message
SR.MustHaveName
What it means
ReferenceExtension.ProvideValue throws InvalidOperationException (SR.MustHaveName) when the extension's Name property is null or empty. {x:Reference} requires a non-empty name to pass to IXamlNameResolver.Resolve. The check runs after the name-resolver service is obtained, before any lookup.
Solutions
- Set Name (or use the ReferenceExtension(string) constructor) with a non-empty value before ProvideValue.
- In XAML, always supply the Name attribute on x:Reference elements.
- Guard dynamic scenarios: skip creating the extension when the source name is empty.
Example fix
// before
var ext = new ReferenceExtension();
var v = ext.ProvideValue(provider); // throws: Name is null
// after
var ext = new ReferenceExtension("btnOk");
var v = ext.ProvideValue(provider); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(ext.Name))
throw new InvalidOperationException("ReferenceExtension.Name must be a non-empty string before ProvideValue"); Type guard
bool IsUsableReference(ReferenceExtension ext) => ext is not null && !string.IsNullOrEmpty(ext.Name);
Try / catch
try { var v = ext.ProvideValue(provider); }
catch (InvalidOperationException) { /* Name unset — supply or skip the reference */ } Prevention
- Use the ReferenceExtension(string) constructor so Name cannot be forgotten.
- Validate XAML sources so every x:Reference carries a Name attribute.
- In generated XAML, substitute or remove placeholders whose names resolve to empty.
When it happens
Trigger: Creating a ReferenceExtension via the parameterless constructor (or with Name = null/"") and calling ProvideValue without setting Name.
Common situations: XAML with <x:Reference> missing the Name attribute; dynamic construction of the extension where the name binding resolved to an empty string; template/generated XAML with unfilled placeholders.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- InvalidOperationException(SR.XamlTypeNameNameIsNullOrEmpty)
- SR.MissingNameResolver
- SR.MustHaveName
- ' '.' ' is a property without a getter and is not a valid…
- ' '.' ' is a property without a getter and is not a valid…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/03fceae26f598d57.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/Reference.cs:35
public Reference(string name)
{
Name = name;
}
[ConstructorArgument("name")]
public string Name { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
ArgumentNullException.ThrowIfNull(serviceProvider);
if (serviceProvider.GetService(typeof(IXamlNameResolver)) is not IXamlNameResolver nameResolver)
{
throw new InvalidOperationException(SR.MissingNameResolver);
}
if (string.IsNullOrEmpty(Name))
{
throw new InvalidOperationException(SR.MustHaveName);
}
object obj = nameResolver.Resolve(Name);
if (obj is null)
{
string[] names = new string[] { Name };
obj = nameResolver.GetFixupToken(names, true);
}
return obj;
}
}
}
View on GitHub (pinned to 81131a70a4)