Created
June 19, 2019 14:51
-
-
Save mgravell/b7866e585d7876e1c1495341cef61176 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static Task<TResponse> Execute<TRequest, TResponse>(this Channel channel, TRequest request, string serviceName, string methodName, | |
CallOptions options = default, string? host = null) | |
where TRequest : class | |
where TResponse : class | |
=> Execute<TRequest, TResponse>(new DefaultCallInvoker(channel), request, serviceName, methodName, options, host); | |
static async Task<TResponse> Execute<TRequest, TResponse>(this CallInvoker invoker, TRequest request, string serviceName, string methodName, | |
CallOptions options = default, string? host = null) | |
where TRequest : class | |
where TResponse : class | |
{ | |
var method = new Method<TRequest, TResponse>(MethodType.Unary, serviceName, methodName, | |
CustomMarshaller<TRequest>.Instance, CustomMarshaller<TResponse>.Instance); | |
using (var auc = invoker.AsyncUnaryCall(method, host, options, request)) | |
{ | |
return await auc.ResponseAsync; | |
} | |
} | |
class CustomMarshaller<T> : Marshaller<T> | |
{ | |
public static readonly CustomMarshaller<T> Instance = new CustomMarshaller<T>(); | |
private CustomMarshaller() : base(Serialize, Deserialize) { } | |
private static T Deserialize(byte[] payload) | |
{ | |
using (var ms = new MemoryStream(payload)) | |
{ | |
return ProtoBuf.Serializer.Deserialize<T>(ms); | |
} | |
} | |
private static byte[] Serialize(T payload) | |
{ | |
using (var ms = new MemoryStream()) | |
{ | |
ProtoBuf.Serializer.Serialize<T>(ms, payload); | |
return ms.ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment