diff --git a/AustinHarris.JsonRpcTest/UnitTest1.cs b/AustinHarris.JsonRpcTest/UnitTest1.cs index 4607f54..f4d2697 100644 --- a/AustinHarris.JsonRpcTest/UnitTest1.cs +++ b/AustinHarris.JsonRpcTest/UnitTest1.cs @@ -128,6 +128,19 @@ public void StringToThrowJsonRpcException() } + [TestMethod] + public void TestLotsOfParams() + { + string request = @"{method:'BuildAListOfWhatever',params:[1,4,7,8,9,123,1.32,""hello""],id:1}"; + string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":[1,4,7,8,9,123,1.32,\"hello\"],\"id\":1}"; + var result = JsonRpcProcessor.Process(request); + result.Wait(); + Assert.IsTrue(result.Result.StartsWith(expectedResult)); + + } + + + [TestMethod] public void ReturnsDateTime() { diff --git a/AustinHarris.JsonRpcTest/service.cs b/AustinHarris.JsonRpcTest/service.cs index 46c2125..62d4745 100644 --- a/AustinHarris.JsonRpcTest/service.cs +++ b/AustinHarris.JsonRpcTest/service.cs @@ -51,6 +51,11 @@ private List CustomStringToListOfString(CustomString input) } + [JsonRpcMethod] + private List BuildAListOfWhatever(int param1, int param2, int param3, int param4, int param5, int param6, double doubleparam, string stringparam) + { + return new List() { param1, param2,param3, param4, param5, param6, doubleparam,stringparam }; + } [JsonRpcMethod("internal.echo")] private string Handle_Echo(string s) diff --git a/Json-Rpc/AustinHarris.JsonRpc.csproj b/Json-Rpc/AustinHarris.JsonRpc.csproj index 3f7907f..cc80c8e 100644 --- a/Json-Rpc/AustinHarris.JsonRpc.csproj +++ b/Json-Rpc/AustinHarris.JsonRpc.csproj @@ -69,21 +69,35 @@ + + ..\packages\FSharp.Core.3.0.0.2\lib\net40\FSharp.Core.dll + + + ..\packages\Jaam.Rpc.1.0.0.4\lib\net40\JAAM.Collections.dll + + + ..\packages\Jaam.Rpc.1.0.0.4\lib\net40\JAAM.RPC.dll + False ..\packages\Newtonsoft.Json.6.0.3\lib\net40\Newtonsoft.Json.dll + + ..\packages\FSharp.Core.3.0.0.2\lib\net40\policy.2.3.FSharp.Core.dll + - + + Designer + - \ No newline at end of file diff --git a/Json-Rpc/Handler.cs b/Json-Rpc/Handler.cs index 3136c4f..b818177 100644 --- a/Json-Rpc/Handler.cs +++ b/Json-Rpc/Handler.cs @@ -13,8 +13,7 @@ public class Handler { #region Members - - //private static Handler current; + private JAAM.RPC.RPCService _rpc = new JAAM.RPC.RPCService(); private static ConcurrentDictionary _sessionHandlers; private static string _defaultSessionId; #endregion @@ -33,7 +32,6 @@ private Handler(string sessionId) { SessionId = sessionId; this.MetaData = new SMD(); - this.Handlers = new Dictionary(); } #endregion @@ -73,7 +71,7 @@ public static void DestroySession(string sessionId) { Handler h; _sessionHandlers.TryRemove(sessionId,out h); - h.Handlers.Clear(); + h._rpc = null; h.MetaData.Services.Clear(); } /// @@ -140,7 +138,6 @@ private void RemoveRpcException() private AustinHarris.JsonRpc.PreProcessHandler externalPreProcessingHandler; private Func externalErrorHandler; private Func parseErrorHandler; - private Dictionary Handlers { get; set; } #endregion /// @@ -159,18 +156,16 @@ private void RemoveRpcException() public bool Register(string key, Delegate handle) { var result = false; - - if (!this.Handlers.ContainsKey(key)) - { - this.Handlers.Add(key, handle); - } + var parameters = Enumerable.Concat(handle.Method.GetParameters().Select(x => Tuple.Create(x.Name, x.ParameterType)),new [] {Tuple.Create("returns", handle.Method.ReturnType)}); + _rpc.AddToService(key, parameters, handle); return result; } public void UnRegister(string key) { - this.Handlers.Remove(key); + Tuple value; + _rpc.handlers.TryRemove(key, out value); MetaData.Services.Remove(key); } @@ -192,14 +187,8 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null) } SMDService metadata = null; - Delegate handle = null; - var haveDelegate = this.Handlers.TryGetValue(Rpc.Method, out handle); var haveMetadata = this.MetaData.Services.TryGetValue(Rpc.Method, out metadata); - - if (haveDelegate == false || haveMetadata == false || metadata == null || handle == null) - { - return new JsonResponse() { Result = null, Error = new JsonRpcException(-32601, "Method not found", "The method does not exist / is not available."), Id = Rpc.Id }; - } + if (Rpc.Params is ICollection == false) { return new JsonResponse() @@ -306,8 +295,29 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null) try { - var results = handle.DynamicInvoke(parameters); - var last = parameters.Length>0 ? parameters[paramCount - 1]:null; + + + var response = _rpc.Invoke(new JAAM.RPC.RpcRequest(Rpc.Method,parameters,Rpc.Id.GetHashCode())); + if (haveMetadata == false || metadata == null) + { + return new JsonResponse() { Result = null, Error = new JsonRpcException(-32601, "Method not found", "The method does not exist / is not available."), Id = Rpc.Id }; + } + + if (response.Error != null) + { + // this is not right yet.. + var testException =(response.Error.data as Exception); + if (testException != null && testException.InnerException is JsonRpcException) + { + return new JsonResponse() { Error = ProcessException(Rpc, testException.InnerException as JsonRpcException) }; + } + // this at least works a little. + return new JsonResponse() { Error = ProcessException(Rpc, new JsonRpcException(-32603, "Internal Error", response.Error)), Id = Rpc.Id }; + } + + var results = response.Result; + + var last = parameters.Length > 0 ? parameters[paramCount - 1]:null; JsonRpcException contextException; if (Task.CurrentId.HasValue && RpcExceptions.TryRemove(Task.CurrentId.Value, out contextException)) { diff --git a/Json-Rpc/packages.config b/Json-Rpc/packages.config index 12fef57..74411a7 100644 --- a/Json-Rpc/packages.config +++ b/Json-Rpc/packages.config @@ -1,4 +1,6 @@  + + \ No newline at end of file diff --git a/NuGet.config b/NuGet.config new file mode 100644 index 0000000..806a057 --- /dev/null +++ b/NuGet.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file