From 7d6b2477806217800b51db13bb8f98b394cd735f Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 7 Nov 2016 23:55:54 -0800 Subject: [PATCH] Move argument conversion for json into JsonNetInvocationAdapter --- .../HubEndPoint.cs | 8 +---- .../JsonNetInvocationAdapter.cs | 34 +++++++++++++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs b/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs index a92f6a0586..1b22100e90 100644 --- a/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs +++ b/src/Microsoft.AspNetCore.SignalR/HubEndPoint.cs @@ -184,13 +184,7 @@ namespace Microsoft.AspNetCore.SignalR try { - var arguments = invocationDescriptor.Arguments ?? EmptyArray; - - var args = arguments - .Zip(parameters, (a, p) => Convert.ChangeType(a, p.ParameterType)) - .ToArray(); - - var result = methodInfo.Invoke(hub, args); + var result = methodInfo.Invoke(hub, invocationDescriptor.Arguments); var resultTask = result as Task; if (resultTask != null) { diff --git a/src/Microsoft.AspNetCore.SignalR/JsonNetInvocationAdapter.cs b/src/Microsoft.AspNetCore.SignalR/JsonNetInvocationAdapter.cs index 13b99e8cd1..7f01f906ee 100644 --- a/src/Microsoft.AspNetCore.SignalR/JsonNetInvocationAdapter.cs +++ b/src/Microsoft.AspNetCore.SignalR/JsonNetInvocationAdapter.cs @@ -3,8 +3,10 @@ using System; using System.IO; +using System.Reflection; using System.Threading.Tasks; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace Microsoft.AspNetCore.SignalR { @@ -16,11 +18,30 @@ namespace Microsoft.AspNetCore.SignalR { } - public async Task ReadInvocationDescriptorAsync(Stream stream, Func getParams) + public Task ReadInvocationDescriptorAsync(Stream stream, Func getParams) { var reader = new JsonTextReader(new StreamReader(stream)); // REVIEW: Task.Run() - return await Task.Run(() => _serializer.Deserialize(reader)); + return Task.Run(() => + { + var jsonInvocation = _serializer.Deserialize(reader); + var invocation = new InvocationDescriptor + { + Id = jsonInvocation.Id, + Method = jsonInvocation.Method, + }; + + var paramTypes = getParams(jsonInvocation.Method); + invocation.Arguments = new object[paramTypes.Length]; + + for (int i = 0; i < paramTypes.Length; i++) + { + var paramType = paramTypes[i]; + invocation.Arguments[i] = jsonInvocation.Arguments[i].ToObject(paramType, _serializer); + } + + return invocation; + }); } public Task WriteInvocationResultAsync(InvocationResultDescriptor resultDescriptor, Stream stream) @@ -41,5 +62,14 @@ namespace Microsoft.AspNetCore.SignalR _serializer.Serialize(writer, value); writer.Flush(); } + + private class JsonNetInvocationDescriptor + { + public string Id { get; set; } + + public string Method { get; set; } + + public JArray Arguments { get; set; } + } } }