I have a method that serialize communication data. First the parameters is serialized into the main CallInformation object as string, then the CallInformation object will also be serialized and then later written to file :
_jSONSettings = new System.Runtime.Serialization.Json.DataContractJsonSerializerSettings();
_jSONSettings.DateTimeFormat = new DateTimeFormat("yyyy-MM-ddThh:mm:ss.fffZ");
_xmlWriterSettings = new System.Xml.XmlWriterSettings() { Indent = true };
var callInformation = logEvent.Properties.Values.First() as CallInformation;
DataContractJsonSerializer serializer;
if (TakeCharsInParameterObject > 0)
{
var counter = 0;
foreach (object param in callInformation.Parameters)
{
using (var stream = new MemoryStream())
{
serializer = new DataContractJsonSerializer(param.GetType(), _jSONSettings);
using (var xmlWriter = System.Xml.XmlWriter.Create(stream, _xmlWriterSettings))
{
serializer.WriteObject(xmlWriter, param);
stream.Flush();
stream.Position = SkipCharsInParameterObject;
using (var streamReader = new StreamReader(stream))
{
var buffer = new char[TakeCharsInParameterObject];
if (streamReader.Peek() >= 0)
{
streamReader.Read(buffer, 0, buffer.Length);
counter++;
callInformation.SerializedParameters += "{Parameter" + counter + ": " + new string(buffer) + "}";
}
}
}
}
}
}
var restult = "";
using (var stream = new MemoryStream())
{
serializer = new DataContractJsonSerializer(typeof(CallInformation), _jSONSettings);
using (var xmlWriter = System.Xml.XmlWriter.Create(stream, _xmlWriterSettings))
{
serializer.WriteObject(xmlWriter, callInformation);
stream.Flush();
stream.Position = 0;
using (var streamReader = new StreamReader(stream))
{
if (streamReader.Peek() >= 0)
restult = streamReader.ReadToEnd();
}
}
}
return restult;
The serialization for parameters works great but the second part do not. After stream.WriteObject the lengh of the stream and the position is still 0?
The CallInformation is a simple DataContract class that looks like this :
[DataContract]
public class CallInformation
{
public CallInformation()
{ }
[DataMember]
public string Address { get; set; }
[DataMember]
public Boolean IsEmpty { get; set; }
[DataMember]
public Boolean IsFaulted { get; set; } = false;
[DataMember]
public string Action { get; set; }
[DataMember]
public CallOrder CallDirection { get; set; }
public DateTime EventTime { get; set; } = DateTime.Now;
[DataMember]
public TimeSpan Duration { get; set; }
[DataMember]
public Boolean IsCallback { get; set; } = false;
[DataMember]
public string LogSource { get; set; } = "Unknown";
[DataMember]
public string SerializedParameters { get; set; } = "";
public List<object> Parameters { get; set; } = new List<object>();
[DataMember]
public string EventTimeDisplay
{
get { return EventTime.ToString("HH:mm:ss.fffffff"); }
set { }
}
}
Why is not the second serialization working?
XmlWriter has got internal buffer, you need to flush it to have stream position changed:
serializer.WriteObject(xmlWriter, callInformation);
xmlWriter.Flush();
stream.Position = 0;