问题
Can someone give me good explanation why this has to fail?
const int bufferSize = 2 * 1024, testValue = 123456;
var buffer = new byte[bufferSize];
var serializer = new DataContractSerializer(typeof(int));
//Serialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(memStream))
serializer.WriteObject(writer, testValue);
//Deserialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memStream, XmlDictionaryReaderQuotas.Max))
{
object deserializedValue = serializer.ReadObject(reader); // <--- nope, this throws System.Runtime.Serialization.SerializationException: The input source is not correctly formatted.
Console.WriteLine(deserializedValue);
}
I'm playing with shared memory IPC and part of it means you have to deal with fixed size inter-process buffer. I'm serializing objects into the buffer and I wanted to be cool so I tried to use DataContractSerializer + binary XmlDictionaryWriter combo, which is one of the fastest non-custom serialization techniques I know of. Problem is when deserializing, it seems XmlDictionaryReader is trying to treat entire memory stream as a big xml document and reads past it's own end-of-stream/block mark, encounters big pile of zeroes and simply craps itself. BinaryFormatter doesn't have this problem as it reads the stream block by block. I had to come up with rather lame solution of implementing custom stream that "fakes" the end of stream after reaching first 0 (assumed to be eof mark of XmlDictionaryWriter).
Complete demo:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
namespace SerializationTest
{
public static class Program
{
public static void Main()
{
const int bufferSize = 2 * 1024, testValue = 123456;
var buffer = new byte[bufferSize];
var serializer = new DataContractSerializer(typeof(int));
//Serialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(memStream))
serializer.WriteObject(writer, testValue);
//Deserialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memStream, XmlDictionaryReaderQuotas.Max))
{
object deserializedValue = serializer.ReadObject(reader); // <--- nope, this throws System.Runtime.Serialization.SerializationException: The input source is not correctly formatted.
Console.WriteLine(deserializedValue);
}
//Deserialize value via FakeEndStream
using (var memStream = new FakeEndStream(new MemoryStream(buffer)))
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memStream, XmlDictionaryReaderQuotas.Max))
{
object deserializedValue = serializer.ReadObject(reader);
Console.WriteLine(deserializedValue);
}
}
private sealed class FakeEndStream : Stream
{
private readonly Stream _source;
private bool _endOfStream;
public FakeEndStream(Stream source)
{
_source = source;
}
#region The workaround
public override int Read(byte[] buffer, int offset, int count)
{
int i = 0;
for (int position = offset; i < count; i++, position++)
{
int value = ReadByte();
if (value < 0)
return i;
buffer[position] = (byte)value;
}
return i;
}
public override int ReadByte()
{
if (_endOfStream)
return -1;
int value = _source.ReadByte();
if (value <= 0)
_endOfStream = true;
return value;
}
#endregion
#region Boilerplate overrides of Stream
protected override void Dispose(bool disposing)
{
_source.Dispose();
}
public override void Flush()
{
_source.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
return _source.Seek(offset, origin);
}
public override void SetLength(long value)
{
_source.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
_source.Write(buffer, offset, count);
}
public override bool CanRead
{
get { return _source.CanRead; }
}
public override bool CanSeek
{
get { return _source.CanSeek; }
}
public override bool CanWrite
{
get { return _source.CanWrite; }
}
public override long Length
{
get { return _source.Length; }
}
public override long Position
{
get { return _source.Position; }
set { _source.Position = value; }
}
#endregion
}
}
}
回答1:
Sure I'll help you out with this (since no one else could be bothered to even confirm/deny the symptoms).
I've found the problem indeed being the reader reading invalid data. However you can help it by writing a whitespace at the end of stream, like so:
//Serialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(memStream))
{
serializer.WriteObject(writer, testValue);
writer.WriteWhitespace(" ");
}
//Deserialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memStream, XmlDictionaryReaderQuotas.Max))
{
object deserializedValue = serializer.ReadObject(reader); // \o/
Console.WriteLine(deserializedValue);
}
Can't believe no one so much as favorited this question as it potentially leads to hard to debug exceptions at runtime and is thus making serializing objects with XmlDictionaryWriter a wee bit unreliable.
Connect report: https://connect.microsoft.com/VisualStudio/feedback/details/811170/xmlbinaryreader-not-able-to-read-from-fixed-size-buffer
回答2:
You need to add Flush() command:
//Serialize value
using (var memStream = new MemoryStream(buffer))
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(memStream))
{
serializer.WriteObject(writer, testValue);
writer.Flush();
}
Source: https://msdn.microsoft.com/ru-ru/library/ms752244(v=vs.110).aspx?f=255&MSPPError=-2147217396
来源:https://stackoverflow.com/questions/20429484/xmldictionaryreader-reading-fixed-size-zeroed-stream