var orderModel = new OrderModel {
OrderType = "BUY",
Price = 2400,
Lot = 5
};
orderModel.ToJson(); //work fine
orderModel.ToXml(); //error: Cannot access a closed Stream.
but i get an exception message:
Cannot access a closed Stream.
can you tell me what's wrong with my code? fyi, ToJson working fine.
so i suppose to use ToXml method just like ToJson but it seem i'm
wrong.
anyway, tq for your great work.
greeting from indonesia.
heri
public void Can_toJson_than_toXml()
{
var orderModel = new OrderModel
{
OrderType = "BUY",
Price = 2400,
Lot = 5
};
var json = orderModel.ToJson();
var fromJson = json.FromJson<OrderModel>();
Assert.That(fromJson.OrderType,
Is.EqualTo(orderModel.OrderType));
var xml = orderModel.ToXml();
var fromXml = xml.FromXml<OrderModel>();
Assert.That(fromXml.OrderType,
Is.EqualTo(orderModel.OrderType));
}
Testing started:
Searching for tests...
Total tests: 1, filtered: 1
Can_toJson_than_toXml has failed:
System.Runtime.Serialization.SerializationException : Error
serializing object of type TestRedis.Program+OrderModel
----> System.ObjectDisposedException : Cannot access a closed
Stream.
C:\src\ServiceStack.Text\src\ServiceStack.Text\XmlSerializer.cs(99,
0) : ServiceStack.Text.XmlSerializer.SerializeToString[T](T from)
C:\src\ServiceStack.Text\src\ServiceStack.Text
\StringExtensions.cs(397, 0) :
ServiceStack.Text.StringExtensions.ToXml[T](T obj)
D:\projects2\TestRedis\TestRedis\Program.cs(50, 0) :
TestRedis.Program.Can_toJson_than_toXml()
C:\src\ServiceStack.Text\src\ServiceStack.Text\XmlSerializer.cs(92,
0) : ServiceStack.Text.XmlSerializer.SerializeToString[T](T from)
Passed: 0, Failed: 1, Ignored: 0
Duration : 1.53349532919818
i used ServiceStack.Text version 3.09 from Nuget package on Windows 7
64 bit, VS 2010 sp1.
thanks,
heri
On Dec 13, 12:32 am, Demis Bellot <demis.bel...@gmail.com> wrote:
> Hi,
>
> I can't replicate this issue, I've added a test case for it here:https://github.com/ServiceStack/ServiceStack.Text/blob/master/tests/S...
>
> Are you using the latest version? otherwise can you send me a complete
> failing unit test that fails. Also what is your platform? i.e. Windows or
> Unix?
>
> Thanks,
>
https://github.com/ServiceStack/ServiceStack.Text/issues/46
tq
On Dec 13, 2:23 pm, Demis Bellot <demis.bel...@gmail.com> wrote:
> Hi,
>
> In this case I have no idea what the problem is as the same test works on
> my home Win7 32bit and works Win7 64bit pcs both running VS.NET 2010 (with
> and without admin mode).
>
> I'd say something is wrong with your environment and the Exception you see
> is probably hiding an inner exception that's the real cause.
> I recommend you try running the source code yourself so you can
> debug/inspect the errors in your IDE:https://github.com/ServiceStack/ServiceStack.Text/blob/master/src/Ser...
>
> private static readonly XmlWriterSettings XSettings = new
> XmlWriterSettings();
>
> public static string SerializeToString<T>(T from)
> {
> try
> {
> using (var ms = new MemoryStream())
> {
> using (var xw = XmlWriter.Create(ms, XSettings))
> {
> var serializer = new
> DataContractSerializer(from.GetType());
> serializer.WriteObject(xw, from);
> xw.Flush();
> ms.Seek(0, SeekOrigin.Begin);
> using (var reader = new StreamReader(ms))
> {
> return reader.ReadToEnd();
> }
> }
> }
> }
> catch (Exception ex)
> {
> throw new SerializationException(string.Format("Error
> serializing object of type {0}", from.GetType().FullName), ex);
> }
> }
>
> I suspect that it might be a permissions problem as I believe Microsofts
> Xml DataContractSerializer generates and stores Reflection.Emit'ed
> assemblies in a tmp folder which you may possibly not have write permission
> to for some reason - although this is a complete guess so you'll have to
> debug the issue on your end.
>
> Cheers,
>
so changing this:
using (var reader = new StreamReader(ms))
{
return reader.ReadToEnd();
}
to this:
var reader = new StreamReader(ms);
return reader.ReadToEnd();
will solve the problem.
regards,
heri