Justin Lilly
Justin Lilly

Reputation: 147

How to serialize to a file with Microsoft Bond

The Input and output streams section of the Microsoft Bond documentation provides this sample code to deserialize from a file:

using (var stream = new FileStream("example.bin", FileMode.Open))
{
    var input = new InputStream(stream);
    var reader = new CompactBinaryReader<InputStream>(input);
    var example = Deserialize<Example>.From(reader);
}

I tried the reverse to serialize to file, but nothing is written to the file.

using (var stream = new FileStream("example.bin", FileMode.Create))
{
    var output = new OutputStream(stream);
    var writer = new CompactBinaryWriter<OutputStream>(output);  
    Serialize.To(writer, example);
}

Any ideas?

Upvotes: 2

Views: 1885

Answers (1)

chwarr
chwarr

Reputation: 7202

It looks like the OutputStream hasn't been flushed into the FileStream. Try adding an explicit call to OutputStream.Flush like the stream example does:

using (var stream = new FileStream("example.bin", FileMode.Create))
{
    var output = new OutputStream(stream);
    var writer = new CompactBinaryWriter<OutputStream>(output);  
    Serialize.To(writer, example);
    output.Flush();
}

I was not around when OutputStream was designed, so I cannot comment on the decision process that resulted in it not implementing IDisposable.

Upvotes: 4

Related Questions