dotnet/wpf · error · ArgumentException
SR.BamlWriterBadStream
Error message
SR.BamlWriterBadStream
What it means
The BamlWriter constructor validates the target stream with ArgumentNullException.ThrowIfNull and then ArgumentException(SR.BamlWriterBadStream) when stream.CanWrite is false. You passed a read-only or closed stream, so BAML output cannot be written.
Solutions
- Open the stream with write access: new FileStream(path, FileMode.Create, FileAccess.Write)
- Check stream.CanWrite before constructing BamlWriter and surface a friendly error
- Create a fresh output stream; do not reuse disposed or input streams
- For null, ensure the stream-producing call succeeded before construction
Example fix
// before
var s = File.OpenRead("out.baml"); var w = new BamlWriter(s);
// after
var s = File.Open("out.baml", FileMode.Create, FileAccess.Write); var w = new BamlWriter(s); Defensive patterns
Strategy: validation
Validate before calling
if (stream == null) throw new ArgumentNullException(nameof(stream));
if (!stream.CanWrite) throw new ArgumentException("Stream must be writable for BamlWriter", nameof(stream));
var writer = new BamlWriter(stream); Type guard
static bool IsWritableStream(Stream s) => s is { CanWrite: true }; Try / catch
try { var w = new BamlWriter(stream); }
catch (ArgumentNullException) { /* stream null — fix producer */ }
catch (ArgumentException ex) { throw new InvalidOperationException("Provide a writable stream for BAML output", ex); } Prevention
- Always open output streams with FileAccess.Write
- Check CanWrite before constructing writers
- Never pass disposed/read streams to BamlWriter
When it happens
Trigger: new BamlWriter(stream) where stream is null, read-only (e.g. FileStream opened FileAccess.Read), or a closed/disposed stream whose CanWrite returns false.
Common situations: Opening a file with FileMode.Open/Read access and handing it to BamlWriter, passing Console.In or a memory stream built for reading, reusing a stream already disposed elsewhere.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Animation_ChildMustBeKeyFrame
- Animation_DependencyPropertyIsNotAnimatable
- Animation_UnrecognizedHandoffBehavior
- ArgumentOutOfRangeException(nameof(offset))
- Can't Assign to Known Type attributes
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d901b98752079e4b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs:31
namespace System.Windows.Markup
{
/// <summary>
/// Writes BAML records to Stream and exposes an XmlWriter-liker interface for BAML
/// </summary>
internal class BamlWriter : IParserHelper
{
#region Constructor
/// <summary>
/// Create a BamlWriter on the passed stream. The stream must be writable.
/// </summary>
public BamlWriter(
Stream stream)
{
ArgumentNullException.ThrowIfNull(stream);
if (!stream.CanWrite)
{
throw new ArgumentException(SR.BamlWriterBadStream);
}
_parserContext = new ParserContext();
if (null == _parserContext.XamlTypeMapper)
{
_parserContext.XamlTypeMapper = new BamlWriterXamlTypeMapper(XmlParserDefaults.GetDefaultAssemblyNames(),
XmlParserDefaults.GetDefaultNamespaceMaps());
}
_xamlTypeMapper = _parserContext.XamlTypeMapper;
_bamlRecordWriter = new BamlRecordWriter(stream, _parserContext, true);
_startDocumentWritten = false;
_depth = 0;
_closed = false;
_nodeTypeStack = new ParserStack();
_assemblies = new Hashtable(7);
_extensionParser = new MarkupExtensionParser((IParserHelper)this, _parserContext);
_markupExtensionNodes = new ArrayList();
}View on GitHub (pinned to 81131a70a4)