-
Hi All, I'm trying to use StreamJsonRpc with MessagePack binary formatter and named pipes. Unfortunately there are no comprehensive examples and when I try to combine what I find in code I get errors. To start with, I made it with standard formatting and it works perfectly. Now I'd like to switch it to MessagePack because there will be big amounts of data sent and performance would be critical. It requires custom formatters of custom types, otherwise I immediately get a serialization error. (Btw, primitive classes with get/set properties were processed without them in a basic app but not with MessagePack). So I do approximatively the following (simplified code): // Service interface:
public interface IService
{
Task<MyClass[]> GetMyClassessAsync();
}
public class MyClass
{
public MyClass() { }
// enum property
public CLASS Class { get; set; } = CLASS.MYCLASS;
// collection property
public List<string> SomeStupidIDs { get; set; }
};
public class MyClassFormatter : IMessagePackFormatter<MyClass>
{
public MyClass Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
{
return new MyClass
{
Class = (CLASS)reader.ReadByte(),
SomeStupidIDs = reader.ReadString().Split('\t').ToList()
};
}
public void Serialize(ref MessagePackWriter writer, MyClass value, MessagePackSerializerOptions options)
{
writer.Write((byte)value.Class);
writer.Write(string.Join("\t", value.SomeStupidIDs));
}
}
//...
// server code:
var stream = new NamedPipeServerStream("MyPipe", PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
var resolver = CompositeResolver.Create(
new IMessagePackFormatter[] { new MyClassFormatter() },
new IFormatterResolver[] { TypelessContractlessStandardResolver.Instance });
var formatter = new MessagePackFormatter();
formatter.SetMessagePackSerializerOptions(MessagePackSerializerOptions.Standard.WithResolver(resolver));
// Not sure is is correct to pass duplex stream twice, but what are the options?
var handler = new LengthHeaderMessageHandler(stream, stream, formatter);
using (var jsonRpc = new JsonRpc(handler, service))
{
jsonRpc.StartListening();
jsonRpc.Completion.Wait();
}
// ...
// client code:
var resolver = CompositeResolver.Create(
new IMessagePackFormatter[] { new MyClassFormatter() },
new IFormatterResolver[] { TypelessContractlessStandardResolver.Instance });
var formatter = new MessagePackFormatter();
formatter.SetMessagePackSerializerOptions(MessagePackSerializerOptions.Standard.WithResolver(resolver));
var stream = new NamedPipeClientStream(".", "MyPipe", PipeDirection.InOut, PipeOptions.Asynchronous);
await stream.ConnectAsync();
// Not sure is is correct to pass duplex stream twice, but what are the options?
var handler = new LengthHeaderMessageHandler(stream, stream, formatter);
var client = JsonRpc.Attach<IService>(handler);
MyClass[] result = await client.GetMyClassessAsync(); When I execute server and client, what happens on server side: Connection is established. What happens on client side: The same stream used with standard formatter worked fine. Any ideas what could be wrong? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Looks like I found a problem. Missing [Read/Write]ArrayHeader in serializer. |
Beta Was this translation helpful? Give feedback.
Looks like I found a problem. Missing [Read/Write]ArrayHeader in serializer.