public static InvocationResultDescriptor DeserializeInvocationResultDescriptor( string jsonString, Dictionary <string, InvocationRequest> handlers) { using (var stringReader = new StringReader(jsonString)) { using (var textReader = new JsonTextReader(stringReader)) { var json = _serializer.Deserialize <JObject>(textReader); if (json == null) { return(null); } var id = json.Value <string>("Id"); var returnType = handlers[id].ResultType; if (!handlers.ContainsKey(id)) { throw new InvalidInvocationResultException(jsonString, id); } var invocationResultDescriptor = new InvocationResultDescriptor { Id = id, Result = returnType == null ? null : json["Result"].ToObject(returnType, _serializer), Error = json.Value <string>("Error") }; return(invocationResultDescriptor); } } }
public async Task WriteInvocationResultAsync(InvocationResultDescriptor resultDescriptor, Stream stream) { if (string.IsNullOrEmpty(resultDescriptor.Error)) { await WriteAsync($"RI{resultDescriptor.Id},E{resultDescriptor.Error}\n", stream); } else { await WriteAsync($"RI{resultDescriptor.Id},R{(resultDescriptor.Result != null ? resultDescriptor.Result.ToString() : string.Empty)}\n", stream); } }
public async Task WriteInvocationResultAsync(InvocationResultDescriptor resultDescriptor, Stream stream) { var outputStream = new CodedOutputStream(stream, leaveOpen: true); outputStream.WriteMessage(new RpcMessageKind() { MessageKind = RpcMessageKind.Types.Kind.Result }); var resultHeader = new RpcInvocationResultHeader { Id = int.Parse(resultDescriptor.Id), HasResult = resultDescriptor.Result != null }; if (resultDescriptor.Error != null) { resultHeader.Error = resultDescriptor.Error; } outputStream.WriteMessage(resultHeader); if (string.IsNullOrEmpty(resultHeader.Error) && resultDescriptor.Result != null) { var result = resultDescriptor.Result; if (result.GetType() == typeof(int)) { outputStream.WriteMessage(new PrimitiveValue { Int32Value = (int)result }); } else if (result.GetType() == typeof(string)) { outputStream.WriteMessage(new PrimitiveValue { StringValue = (string)result }); } else { var serializer = _serviceProvider.GetRequiredService <ProtobufSerializer>(); var message = serializer.GetMessage(result); outputStream.WriteMessage(message); } } outputStream.Flush(); await stream.FlushAsync(); }
private void HandleInvokeResult(InvocationResultDescriptor descriptor) { InvocationRequest request; lock (_pendingCallsLock) { request = _pendingCalls[descriptor.Id]; _pendingCalls.Remove(descriptor.Id); } request.Registration.Dispose(); if (!string.IsNullOrEmpty(descriptor.Error)) { request.Completion.TrySetException(new MorseLException(descriptor.Error)); } else { request.Completion.TrySetResult(descriptor.Result); } }