dotnet/wpf · critical · InvalidOperationException
SR.ParserBamlVersion
Error message
SR.ParserBamlVersion: {0} {1} What it means
BamlVersionHeader.LoadVersion throws InvalidOperationException(SR.ParserBamlVersion) when the BAML file's BamlWriterVersion differs from the reader's BamlVersion.ReaderVersion. Only major version compatibility is assumed, so a BAML written by an incompatible major version cannot be read.
Solutions
- Recompile the XAML/BAML with the same .NET version as the deployment runtime
- Ensure the runtime machine has the framework version the app was built against
- Clean stale resources: delete old obj/bin and re-deploy so no old-version BAML ships
- If versions must differ, ship loose XAML (XamlReader.Load) instead of precompiled BAML
Example fix
// before: built on net8, deployed on net6 runtime // after: align versions <TargetFramework>net6.0</TargetFramework> <!-- build and run on same TFM -->
Defensive patterns
Strategy: validation
Validate before calling
// check BAML header version before loading (must match BamlVersion.ReaderVersion major) // read the BamlVersionHeader first; abort with a friendly message if majors differ
Try / catch
try { LoadBaml(stream); }
catch (InvalidOperationException ex) when (ex.Message.Contains("version")) {
throw new DeploymentException("BAML was compiled with an incompatible framework version — redeploy rebuilt assemblies", ex);
} Prevention
- Build TFM must equal deployment runtime TFM
- Clean obj/bin and redeploy on framework upgrades
- Never ship precompiled BAML across different .NET major versions
When it happens
Trigger: Loading a BAML stream whose header major version (from BamlVersionHeader) is not equal to the runtime reader's BamlVersion.ReaderVersion — typically a BAML compiled with a different .NET/WPF major version.
Common situations: Deploying assemblies compiled against a newer .NET than the runtime, or consuming precompiled BAML resources (e.g. from a shared library) with an older framework; framework upgrades where stale satellite/old-build resources remain.
Understand the failure class
Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.
Related errors
- SR.Format(SR.UnknownBamlRecord, recordType)
- SR.ParserCantGetDPOrPi
- SR.ParserUnknownBaml
- Can't Assign to Known Type attributes
- Could not find prefix for type
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b3c1ff55db7c0959.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlVersionHeader.cs:80
{
#if DEBUG
long posStart = bamlBinaryReader.BaseStream.Position;
#endif
BamlVersion = FormatVersion.LoadFromStream(bamlBinaryReader.BaseStream);
#if DEBUG
long posEnd = bamlBinaryReader.BaseStream.Position;
Debug.Assert((posEnd-posStart) == BamlVersionHeader.BinarySerializationSize,
"Incorrect Baml Version Header Size");
#endif
// We're assuming that only major versions are significant for compatibility,
// so if we have a major version in the file that is higher than that in
// the code, we can't read it.
if (BamlVersion.ReaderVersion != BamlWriterVersion)
{
throw new InvalidOperationException(SR.Format(SR.ParserBamlVersion,
(BamlVersion.ReaderVersion.Major.ToString(CultureInfo.CurrentCulture) + "." +
BamlVersion.ReaderVersion.Minor.ToString(CultureInfo.CurrentCulture)),
(BamlWriterVersion.Major.ToString(CultureInfo.CurrentCulture) + "." +
BamlWriterVersion.Minor.ToString(CultureInfo.CurrentCulture))));
}
}
#endif
internal void WriteVersion(BinaryWriter bamlBinaryWriter)
{
#if DEBUG
long posStart = bamlBinaryWriter.BaseStream.Position;
#endif
BamlVersion.SaveToStream(bamlBinaryWriter.BaseStream);
#if DEBUG
long posEnd = bamlBinaryWriter.BaseStream.Position;View on GitHub (pinned to 81131a70a4)