【问题标题】:An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dllmscorlib.dll 中出现“System.Runtime.Serialization.SerializationException”类型的未处理异常
【发布时间】:2015-09-29 05:59:31
【问题描述】:

请帮帮我,我卡住了

未处理的类型异常 'System.Runtime.Serialization.SerializationException' 发生在 mscorlib.dll

附加信息:在解析完成之前遇到流结束。

执行方法时出现上述错误

void ReceiveCallback(IAsyncResult AR)
{
    using (ms = new MemoryStream())
    {
        Socket localsock = (Socket)AR.AsyncState;
        int RecLen = localsock.EndReceive(AR);
        Array.Resize(ref buffer, RecLen);
        bnrFormatter = new BinaryFormatter();                
        ms.Write(buffer, 0, buffer.Length);
        ms.Position = 0;                
        AddListItem("MS length:" + ms.Length + "\nMS position: " + ms.Position);            
        TrackerData data = (TrackerData)bnrFormatter.Deserialize(ms);

        if (pushToDB(data) == 1)
        {
            AddListItem("New packet data received from client of IP " + ((IPEndPoint)localsock.LocalEndPoint).Address.ToString() + " and Port " + ((IPEndPoint)localsock.LocalEndPoint).Port + " .");
        }
        byte[] resBuffer = Encoding.Default.GetBytes("New packet data sent to the server!!");
        localsock.BeginSend(resBuffer, 0, resBuffer.Length, SocketFlags.None, new AsyncCallback(EndCallback), localsock);
        localsock.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), localsock);
    }
}

错误出现在一行

TrackerData data = (TrackerData)bnrFormatter.Deserialize(ms);  

【问题讨论】:

    标签: c# asp.net visual-studio


    【解决方案1】:

    试试这个解决方案。 将内存流的位置设置为 0,不要使用您的对象,而是使用对象类型。

      ms.Position = 0;
      BinaryFormatter bnrFormatter = new BinaryFormatter();
      TrackerData data = (TrackerDataObjectType)bnrFormatter .Deserialize(ms);
    

    【讨论】:

    • 是的,shreesha。你能解释一下错误是什么
    • 将位置设置为 0 会将您带到流的开头。我认为它可能会解决问题
    • 其实问题出在TrackerDataObjectType从哪里出现到屏幕上TrackerData的类是[Serializable] public class TrackerData { public string PacketData { get;放; } 公共日期时间 OnDate { 获取;放; } }
    • ms.Length 和 ms.Position 的值是多少?
    猜你喜欢
    • 2017-09-05
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多