IpcServiceFramework icon indicating copy to clipboard operation
IpcServiceFramework copied to clipboard

customize user data serialization for request and response

Open oleksabor opened this issue 6 years ago • 4 comments

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 avatar Mar 15 '19 15:03 oleksabor

@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.

jacqueskang avatar Mar 28 '19 08:03 jacqueskang

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

oleksabor avatar Apr 02 '19 19:04 oleksabor

I see the issue now. I'll see if I can make serialization customization easier in next version. Thanks

jacqueskang avatar Apr 02 '19 19:04 jacqueskang

Sorry due to lack of time I'm again moving this issue to next milestone

jacqueskang avatar Jun 12 '20 13:06 jacqueskang