[ProtoContract]public class LogData{ [ProtoContract] public enum LogSeverity { [ProtoEnum(name = "information",Value = 0)] information,[ProtoEnum(name = "Warning",Value = 1)] Warning,[ProtoEnum(name = "Error",Value = 2)] Error,[ProtoEnum(name = "Critical",Value = 3)] Critical } [ProtoMember(1)] public string UserID { get; set; } [ProtoMember(2)] public string Computername { get; set; } [ProtoMember(3)] public ExceptionProxy Exception { get; set; } [ProtoMember(4)] public LogData.LogSeverity Severity { get; set; } [ProtoMember(5)] public string Source { get; set; } [ProtoMember(6)] public string Caption { get; set; } [ProtoMember(7)] public string Description { get; set; } [ProtoMember(8)] public DateTime TimeOfOccurrence { get; set; } [ProtoMember(9)] public GuID SessionID { get; set; } [ProtoMember(10)] public string Methodname { get; set; } [ProtoMember(11)] public string Osversion { get; set; } [ProtoMember(12)] public string category { get; set; } [ProtoMember(13)] public string Location { get; set; }}[ProtoContract]public class ExceptionProxy{ [ProtoMember(1)] public Type ExceptionType { get; set; } [ProtoMember(2)] public string Message { get; set; } [ProtoMember(3)] public string StackTrace { get; set; } [ProtoMember(4)] public ExceptionProxy InnerException { get; set; }}
这是我的代码,进行序列化和发送
private voID WriteLogDataToServer(LogData data) { using (var clIEnt = new TcpClIEnt()) { clIEnt.Connect(Host,SignallineserverPort); using (var stream = clIEnt.GetStream()) { using (var ms = new MemoryStream()) { Serializer.Serialize<LogData>(ms,data); var buffer = ms.GetBuffer(); int position = 0; WriteFrameMarkers(stream); byte[] frameLengthBuffer = BitConverter.GetBytes(buffer.Length); stream.Write(frameLengthBuffer,IntByteSize); while (position < buffer.Length) { int length = Math.Min(ChunkSize,buffer.Length - position); stream.Write(buffer,position,length); position += ChunkSize; } } } clIEnt.Close(); } }
这是读取服务器上的数据的代码
public overrIDe LogData ReadData(NetworkStream stream) { if (stream.DataAvailable) { try { const int chunkSize = 250; byte[] buffer = new byte[IntByteSize]; int messageSize = 0; int totalBytesRead = 0; LogData data; using (var ms = new MemoryStream()) { if (!ReadFrameMarkers(stream)) return null; totalBytesRead = stream.Read(buffer,IntByteSize); if (totalBytesRead != IntByteSize) return null; messageSize = BitConverter.ToInt32(buffer,0); totalBytesRead = 0; while ((totalBytesRead < messageSize)) { int bufferSize = Math.Min(chunkSize,messageSize - totalBytesRead); buffer = new byte[bufferSize]; int bytesRead = stream.Read(buffer,bufferSize); if (bytesRead != 0) { totalBytesRead += bytesRead; ms.Write(buffer,bytesRead); } } ms.Seek(0,SeekOrigin.Begin); data = Serializer.Deserialize<LogData>(ms); } return data; } catch (Exception ex) { Console.Writeline(string.Format("Error occurred: {0}",ex.Message)); return null; } } return null; }解决方法 简单的一个:你使用:
var buffer = ms.GetBuffer();
然后buffer.Length.这意味着您正在使用超大型的填充缓冲区.如果你这样做,你需要使用ms.Length,这将告诉你实际的长度.或者,可以使用ms.ToArray(),但这涉及额外的副本.
我的建议:继续使用GetBuffer(),但只能写入ms.Length字节,而不是buffer.Length字节.
一旦你删除了这些额外不正确的零,我期望你会发现它的作品.
总结以上是内存溢出为你收集整理的c# – ProtoBuf-Net和Compact Framework中的“源数据中的无效字段:0”错误全部内容,希望文章能够帮你解决c# – ProtoBuf-Net和Compact Framework中的“源数据中的无效字段:0”错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)