我使用BinaryFormatter序列化/反序列化服务器/客户端上的字符串列表。 但有时会出现异常" System.Runtime.Serialization.SerializationException:在解析完成之前遇到的End of Stream"在反序列化期间出现在客户端:
List<string> retrievedList = (List<string>)formatterDeserialize.Deserialize(reader.BaseStream);
服务器代码:
var server = new NamedPipeServerStream("PipesEnroll", PipeDirection.InOut);
while (true)
{
server.WaitForConnection();
StreamWriter writer = new StreamWriter(server);
List<string> sendList = new List<string>();
sendList.Add(isOk.ToString());
sendList.Add(terminalTemplate);
sendList.Add(matcherTemplate);
sendList.Add(numberFingers.ToString());
BinaryFormatter formatterSerialize = new BinaryFormatter();
formatterSerialize.Serialize(writer.BaseStream, sendList);
writer.Flush();
server.Disconnect();
}
客户代码:
using (var client = new NamedPipeClientStream(".", "PipesEnroll", PipeDirection.InOut))
{
client.Connect();
StreamReader reader = new StreamReader(client);
BinaryFormatter formatterDeserialize = new BinaryFormatter();
List<string> retrievedList = (List<string>) formatterDeserialize.Deserialize(reader.BaseStream);
}
异常详情:
System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed.
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at CIS.BioTime.Manager.EmployeeWizardForm.SetTemplate_Impl(Int32 templateNumberX, ImageSet imageSet, IList`1 bitmapImages, Boolean& zkWarning, Boolean generateZKTemplateOnly) in d:\BioTime\trunk\REL_6.0.0\SOURCE\BioTime\CIS.BioTime.Manager\EmployeeWizardForm.Code.cs:line 1447
有人对如何避免此问题有任何建议吗?