IpcServiceFramework
                                
                                
                                
                                    IpcServiceFramework copied to clipboard
                            
                            
                            
                        customize user data serialization for request and response
Hello
I'm looking for wcf replacement in the .netCore. REST is fine but is too slow when complex data types are being serialized.
It would be nice to have possibility to replace user data JSON serialization with custom one. It can be useful if Protobuf or another framework is used with improved serialization algorithms.
My initial idea was to serialize IpcRequest.Parameters and IpcResponse.Data as byte array and then call DefaultIpcMessageSerializer
However the problem is that not every type is serialized to binary so certain header has to be used to deserialize request\response data properly back to object.
For example, new interface like IIpcMessageDataSerializer can be introduced.
It can be used by IpcReader and IpcWriter to serialize method parameter and|or result value.
Here is IpcWriter class draft to explain it better
private readonly IIpcMessageDataSerializer _dataSerializer;
public IpcWriter(Stream stream, IIpcMessageSerializer serializer, bool leaveOpen, IIpcMessageDataSerializer dataSerializer)
{
    _stream = stream;
    _serializer = serializer;
    _dataSerializer = dataSerializer;
    _leaveOpen = leaveOpen;
}
public async Task WriteAsync(IpcRequest request,
    CancellationToken cancellationToken = default(CancellationToken))
{
	request.Parameters = request.Parameters.Select(_ => _dataSerializer.Serialize(_)).ToArray();
		
    byte[] binary = _serializer.SerializeRequest(request);
    await WriteMessageAsync(binary, cancellationToken);
}
public async Task WriteAsync(IpcResponse response,
    CancellationToken cancellationToken = default(CancellationToken))
{
	response.Data = _dataSerializer.Serialize(_);
	
    byte[] binary = _serializer.SerializeResponse(response);
    await WriteMessageAsync(binary, cancellationToken);
}
And adjust IpcReader as below
public async Task<IpcRequest> ReadIpcRequestAsync(CancellationToken cancellationToken = default(
{
    byte[] binary = await ReadMessageAsync(cancellationToken);
    var req = _serializer.DeserializeRequest(binary);
	req.Parameters = req.Parameters.Select(_ => _dataSerializer.Deserialize(_));
	return res;
}
public async Task<IpcResponse> ReadIpcResponseAsync(CancellationToken cancellationToken = default(
{
    byte[] binary = await ReadMessageAsync(cancellationToken);
    var res = _serializer.DeserializeResponse(binary);
	res.Data = _dataSerializer.Deserialize(res.Data);
	return res;
}
                                    
                                    
                                    
                                
@oleksabor,
To customize serialization you can register your custom implementation of the interface JKang.IpcServiceFramework.IIpcMessageSerializer in IServiceCollection.
It will replace the default implementation DefaultIpcMessageSerializer which uses Newtonsoft.Json
Please notice that currently this will impact all IPC endpoints.
It is not so easy, since Google Protobuf requires objects to be generated from the pseudo code (for example) so IpcRequest and IpcResponse can't be used with Protobuf deserialization
I can serialize IpcRequest parameters as Protobuf objects and IpcReponse.Value, but not the request and the response instances.
Additionally BinarySerializer requires object type class definition to be marked with SerializedAttribute (or implemets ISerializable). And IpcRequest IpcResponse can't be used with this serializer.
I'd like to get some binary serialization rather then using json or xml
I see the issue now. I'll see if I can make serialization customization easier in next version. Thanks
Sorry due to lack of time I'm again moving this issue to next milestone