-
E.g. I want host 1 to get an instance of On host 1 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
On host1: _serverMethods = new Host1();
_jsonRpc = new JsonRpc(new WebSocketMessageHandler(_webSocketStream, new MessagePackFormatter()), _serverMethods)
{
CancelLocallyInvokedMethodsWhenConnectionIsClosed = true
};
ClientMethods = _jsonRpc.Attach<IHost2>();
_jsonRpc.StartListening();
await _jsonRpc.Completion; On host2: _serverMethods = new Host2();
_jsonRpc = new JsonRpc(new `WebSocketMessageHandler(_webSocketStream, new MessagePackFormatter()), _serverMethods)
{
CancelLocallyInvokedMethodsWhenConnectionIsClosed = true
};
ClientMethods = _jsonRpc.Attach<IHost1>();
_jsonRpc.StartListening();
await _jsonRpc.Completion; |
Beta Was this translation helpful? Give feedback.
-
I tried this, but the methods don't get called, and in the trace I get
using System.IO.Pipes;
using StreamJsonRpc;
class Duplex {
public async Task RunAsync() {
var server = new NamedPipeServerStream("JsonRpcDuplex", PipeDirection.InOut, maxNumberOfServerInstances: 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
var client = new NamedPipeClientStream(".", "JsonRpcDuplex", PipeDirection.InOut, PipeOptions.Asynchronous);
await Task.WhenAll(server.WaitForConnectionAsync(), client.ConnectAsync()).ConfigureAwait(false);
var s1 = this.Server1(server, client);
var s2 = this.Server2(client, server);
s1.Do2();
s2.Do1();
}
IHost2 Server1(Stream send, Stream recv) {
var host = new Host1();
var rpc = new JsonRpc(send, recv) {
CancelLocallyInvokedMethodsWhenConnectionIsClosed = true
};
rpc.AddLocalRpcTarget<IHost1>(host, null);
var service2 = rpc.Attach<IHost2>();
rpc.StartListening();
return service2;
}
IHost1 Server2(Stream send, Stream recv) {
var host = new Host2();
var rpc = new JsonRpc(send, recv) {
CancelLocallyInvokedMethodsWhenConnectionIsClosed = true
};
rpc.AddLocalRpcTarget<IHost2>(host, null);
var service1 = rpc.Attach<IHost1>();
rpc.StartListening();
return service1;
}
interface IHost1 {
void Do1();
}
interface IHost2 {
void Do2();
}
public class Host1: IHost1 {
public void Do1() => Debug.WriteLine("Do1");
}
public class Host2: IHost2 {
public void Do2() => Debug.WriteLine("Do2");
}
} P.S. I also tried to pass P.P.S. I have a word to say about a failure to call a method to only raise a warning... P.P.P.S. It throws an exception instead of just printing a warning if I change |
Beta Was this translation helpful? Give feedback.
The pipes were indeed cross-wired, so that each party talked to themself. Here is the corrected version: https://dotnetfiddle.net/MfT0VV
The trick is that named pipes are already full duplex, so you only give one of them to each party -- not both!