private InvocationMessage BindInvocationMessage(JObject json, IInvocationBinder binder) { var invocationId = JsonUtils.GetRequiredProperty <string>(json, InvocationIdPropertyName, JTokenType.String); var target = JsonUtils.GetRequiredProperty <string>(json, TargetPropertyName, JTokenType.String); var nonBlocking = JsonUtils.GetOptionalProperty <bool>(json, NonBlockingPropertyName, JTokenType.Boolean); var args = JsonUtils.GetRequiredProperty <JArray>(json, ArgumentsPropertyName, JTokenType.Array); var paramTypes = binder.GetParameterTypes(target); var arguments = new object[args.Count]; if (paramTypes.Length != arguments.Length) { throw new FormatException($"Invocation provides {arguments.Length} argument(s) but target expects {paramTypes.Length}."); } for (var i = 0; i < paramTypes.Length; i++) { var paramType = paramTypes[i]; // TODO(anurse): We can add some DI magic here to allow users to provide their own serialization // Related Bug: https://github.com/aspnet/SignalR/issues/261 arguments[i] = args[i].ToObject(paramType, _payloadSerializer); } return(new InvocationMessage(invocationId, nonBlocking, target, arguments)); }
private StreamCompletionMessage BindStreamCompletionMessage(JObject json) { var invocationId = JsonUtils.GetRequiredProperty <string>(json, InvocationIdPropertyName, JTokenType.String); var error = JsonUtils.GetOptionalProperty <string>(json, ErrorPropertyName, JTokenType.String); return(new StreamCompletionMessage(invocationId, error)); }
private CompletionMessage BindCompletionMessage(JObject json, IInvocationBinder binder) { var invocationId = JsonUtils.GetRequiredProperty <string>(json, InvocationIdPropertyName, JTokenType.String); var error = JsonUtils.GetOptionalProperty <string>(json, ErrorPropertyName, JTokenType.String); var resultProp = json.Property(ResultPropertyName); if (error != null && resultProp != null) { throw new InvalidDataException("The 'error' and 'result' properties are mutually exclusive."); } CompletionMessage message; if (resultProp == null) { message = new CompletionMessage(invocationId, error, result: null, hasResult: false); } else { var returnType = binder.GetReturnType(invocationId); var payload = resultProp.Value?.ToObject(returnType, PayloadSerializer); message = new CompletionMessage(invocationId, error, result: payload, hasResult: true); } ReadHeaders(json, message.Headers); return(message); }
public static HandshakeResponseMessage ParseResponseMessage(ReadOnlyMemory <byte> payload) { using (var reader = CreateJsonTextReader(payload)) { var token = JToken.ReadFrom(reader); var handshakeJObject = JsonUtils.GetObject(token); // a handshake response does not have a type // check the incoming message was not any other type of message var type = JsonUtils.GetOptionalProperty <string>(handshakeJObject, TypePropertyName); if (!string.IsNullOrEmpty(type)) { throw new InvalidOperationException("Handshake response should not have a 'type' value."); } var error = JsonUtils.GetOptionalProperty <string>(handshakeJObject, ErrorPropertyName); return(new HandshakeResponseMessage(error)); } }
private InvocationMessage BindInvocationMessage(JObject json, IInvocationBinder binder) { var invocationId = JsonUtils.GetOptionalProperty <string>(json, InvocationIdPropertyName, JTokenType.String); var target = JsonUtils.GetRequiredProperty <string>(json, TargetPropertyName, JTokenType.String); var args = JsonUtils.GetRequiredProperty <JArray>(json, ArgumentsPropertyName, JTokenType.Array); var paramTypes = binder.GetParameterTypes(target); try { var arguments = BindArguments(args, paramTypes); return(new InvocationMessage(invocationId, target, argumentBindingException: null, arguments: arguments)); } catch (Exception ex) { return(new InvocationMessage(invocationId, target, ExceptionDispatchInfo.Capture(ex))); } }