Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customize user data serialization for request and response #65

Open
oleksabor opened this issue Mar 15, 2019 · 4 comments
Open

customize user data serialization for request and response #65

oleksabor opened this issue Mar 15, 2019 · 4 comments
Labels
enhancement New feature or request

Comments

@oleksabor
Copy link

oleksabor commented Mar 15, 2019

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;
}
@jacqueskang
Copy link
Owner

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

@oleksabor
Copy link
Author

oleksabor commented Apr 2, 2019

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

@jacqueskang jacqueskang reopened this Apr 2, 2019
@jacqueskang
Copy link
Owner

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

@jacqueskang jacqueskang added this to the 2.3.0 milestone Apr 30, 2019
@jacqueskang jacqueskang added the enhancement New feature or request label Apr 30, 2019
@jacqueskang jacqueskang modified the milestones: 2.3.0, 3.0.0 Apr 28, 2020
@jacqueskang
Copy link
Owner

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

@jacqueskang jacqueskang modified the milestones: 3.0.0, 3.1.0 Jun 12, 2020
@jacqueskang jacqueskang removed this from the 3.1.0 milestone Sep 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants