how can i push message between server and client in namedPipeWrapper?

Viewed 117

I wrote the client and server side based on the documentation Named Pipe Wrapper, but unfortunately only server.ClientConnected runs and server.ClientMessage and client.ServerMessage do not run. How can I push a message from the client to the server And vice versa?

model :

[Serializable]
public class SomeClass
{
    public string Request { get; set; }
    public string Response { get; set; }
}

client side :

var client = new NamedPipeClient<SomeClass>("MyServerPipe");

        client.ServerMessage += delegate (NamedPipeConnection<SomeClass, SomeClass> conn, SomeClass message)
        {
            Console.WriteLine("Server says: {0}", message.Response);
        };

        // Start up the client asynchronously and connect to the specified server pipe.
        // This method will return immediately while the client runs in a separate background thread.
        client.Start();
        Console.ReadKey();

server side:

 var server = new NamedPipeServer<SomeClass>("MyServerPipe");

        server.ClientConnected += delegate (NamedPipeConnection<SomeClass, SomeClass> conn)
        {
            Console.WriteLine("Client {0} is now connected!", conn.Id);
            conn.PushMessage(new SomeClass { Response= "Welcome!" });
        };

        server.ClientMessage += delegate (NamedPipeConnection<SomeClass, SomeClass> conn, SomeClass message)
        {
            Console.WriteLine("Client {0} says: {1}", conn.Id, message.Request);
        };

        // Start up the server asynchronously and begin listening for connections.
        // This method will return immediately while the server runs in a separate background thread.
        server.Start();
        Console.ReadKey();
1 Answers

Based on documentation

To test the performance of the pipe, it is better that you convert the ExampleCLI project into two client and server projects and move the SomeClass model into a new class library. Your problem occurs when the model is serializable. That is why the server.ClientMessage, and client.ServerMessage are not called.

Related