tags:

views:

108

answers:

1

I try to do something really simple in theory : deserialize a string to a Message, here is the code :

[TestMethod]
public void EncoderErrorTest()
{
    var message = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\"&gt;&lt;s:Header&gt;&lt;a:Action s:mustUnderstand=\"1\">http://tempuri.org/IHelloWorldService/SayHello&lt;/a:Action&gt;&lt;/s:Header&gt;&lt;s:Body&gt;&lt;SayHello xmlns=\"http://tempuri.org/\"&gt;&lt;name&gt;Nico&lt;/name&gt;&lt;/SayHello&gt;&lt;/s:Body&gt;&lt;/s:Envelope&gt;";

    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);
    writer.Write(message);
    stream.Position = 0;
    var soapMessage = new TextMessageEncodingBindingElement().CreateMessageEncoderFactory().Encoder.ReadMessage(stream, 99999);
    Assert.IsNotNull(soapMessage);
}

But during the deserialization I get a XmlException "Unexpected end of file". Is something wrong with my code ?

Thanks in advance for your responses.

+4  A: 

Try calling Flush on your StreamWriter before you read from the stream (or better yet, put it in a using-block).

Pavel Minaev
thanks, what a shame -_-
Nicolas Dorier
Around the stream as well.
John Saunders
`MemoryStream` isn't caching, as far as I know (would be pretty pointless - why do a memory cache for a memory-backed stream?), but it wouldn't hurt anyway.
Pavel Minaev
The fewer exceptions to the "use using blocks" rule, the better.
John Saunders