I have two windows applications and using named pipes to send and receive lists of data between them. With serialization/deserialization mechanism using BinaryFormatter class.
Server:
static void StartServer()
{
var server = new NamedPipeServerStream("PipesEnroll", PipeDirection.InOut);
while (true)
{
StreamReader reader = new StreamReader(server);
StreamWriter writer = new StreamWriter(server);
string terminalTemplate;
string matcherTemplate;
int mathVersionNumber = 9;
int numberFingers;
BinaryFormatter formatterDeserialize = new BinaryFormatter();
List<byte[]> retrievedList = (List<byte[]>)formatterDeserialize.Deserialize(reader.BaseStream);
bool isOk = Enroll.EnrollWithoutWCF(retrievedList, mathVersionNumber, out terminalTemplate, out matcherTemplate, out numberFingers);
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);
server.Disconnect();
}
Client:
using (var client = new NamedPipeClientStream(".", "PipesEnroll", PipeDirection.InOut))
{
client.Connect();
StreamReader reader = new StreamReader(client);
StreamWriter writer = new StreamWriter(client);
BinaryFormatter formatterSerialize = new BinaryFormatter();
formatterSerialize.Serialize(writer.BaseStream, images);
// writer.Write(stream);
// writer.Flush();
BinaryFormatter formatterDeserialize = new BinaryFormatter();
List<String> retrievedList = (List<String>)formatterDeserialize.Deserialize(reader.BaseStream);
bool isOK = Convert.ToBoolean(retrievedList[0]);
string terminalTemplate = retrievedList[1];
string matcherTemplate = retrievedList[2];
int numberFingers = Convert.ToInt32(retrievedList[3]);
}
But exception "System.IO.Exception: Pipe is broken" appears on client side when executing formatterSerialize.Serialize(writer.BaseStream, images); in debug mode.
Does anybody have any suggestion on how to avoid this issue?