Move argument conversion for json into JsonNetInvocationAdapter

This commit is contained in:
David Fowler 2016-11-07 23:55:54 -08:00
parent d00f1f93b2
commit 7d6b247780
2 changed files with 33 additions and 9 deletions

View File

@ -184,13 +184,7 @@ namespace Microsoft.AspNetCore.SignalR
try try
{ {
var arguments = invocationDescriptor.Arguments ?? EmptyArray; var result = methodInfo.Invoke(hub, invocationDescriptor.Arguments);
var args = arguments
.Zip(parameters, (a, p) => Convert.ChangeType(a, p.ParameterType))
.ToArray();
var result = methodInfo.Invoke(hub, args);
var resultTask = result as Task; var resultTask = result as Task;
if (resultTask != null) if (resultTask != null)
{ {

View File

@ -3,8 +3,10 @@
using System; using System;
using System.IO; using System.IO;
using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNetCore.SignalR namespace Microsoft.AspNetCore.SignalR
{ {
@ -16,11 +18,30 @@ namespace Microsoft.AspNetCore.SignalR
{ {
} }
public async Task<InvocationDescriptor> ReadInvocationDescriptorAsync(Stream stream, Func<string, Type[]> getParams) public Task<InvocationDescriptor> ReadInvocationDescriptorAsync(Stream stream, Func<string, Type[]> getParams)
{ {
var reader = new JsonTextReader(new StreamReader(stream)); var reader = new JsonTextReader(new StreamReader(stream));
// REVIEW: Task.Run() // REVIEW: Task.Run()
return await Task.Run(() => _serializer.Deserialize<InvocationDescriptor>(reader)); return Task.Run(() =>
{
var jsonInvocation = _serializer.Deserialize<JsonNetInvocationDescriptor>(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) public Task WriteInvocationResultAsync(InvocationResultDescriptor resultDescriptor, Stream stream)
@ -41,5 +62,14 @@ namespace Microsoft.AspNetCore.SignalR
_serializer.Serialize(writer, value); _serializer.Serialize(writer, value);
writer.Flush(); writer.Flush();
} }
private class JsonNetInvocationDescriptor
{
public string Id { get; set; }
public string Method { get; set; }
public JArray Arguments { get; set; }
}
} }
} }