diff --git a/.github/workflows/build_publish_master.yml b/.github/workflows/build_publish_master.yml new file mode 100644 index 0000000..1a7fe10 --- /dev/null +++ b/.github/workflows/build_publish_master.yml @@ -0,0 +1,33 @@ +name: Build Master +on: + push: + paths-ignore: + - "**/*.md" + branches: [ master ] + +jobs: + build: + + strategy: + matrix: + os: ['ubuntu-latest'] + dotnet-version: ['3.1.201'] + project : ['Json-Rpc'] + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v2 + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: ${{matrix.dotnet-version}} + - name: Install dependencies + run: dotnet restore + - name: Build + run: dotnet build ${{matrix.project}} --configuration Release + - name: Test + run: dotnet test AustinHarris.JsonRpcTestN + # Publish + - name: publish nuget version change + run: dotnet nuget push Json-Rpc/bin/Release/*.nupkg --skip-duplicate --source "https://www.nuget.org" --api-key ${{secrets.NugetKey}} # API key for the NuGet feed diff --git a/.github/workflows/build_pull_request.yml b/.github/workflows/build_pull_request.yml new file mode 100644 index 0000000..f3e06e9 --- /dev/null +++ b/.github/workflows/build_pull_request.yml @@ -0,0 +1,35 @@ +name: Pull Reqest +on: + pull_request: + paths-ignore: + - "**/*.md" + branches: [ master ] + +jobs: + build: + + strategy: + matrix: + os: ['windows-latest','ubuntu-latest'] + dotnet-version: ['3.1.201'] + project : ['Json-Rpc'] + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v2 + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: ${{matrix.dotnet-version}} + - name: Install dependencies + run: dotnet restore + - name: Build + run: dotnet build ${{matrix.project}} --configuration Release --version-suffix ci-${{ github.run_id }}-${{ github.run_number }} + - name: Test + run: dotnet test AustinHarris.JsonRpcTestN + # Publish + - name: publish nuget version change + if: ${{matrix.os == 'ubuntu-latest'}} + run: dotnet nuget push Json-Rpc/bin/Release/*.nupkg --skip-duplicate --source "https://www.nuget.org" --api-key ${{secrets.NugetKey}} # API key for the NuGet feed + \ No newline at end of file diff --git a/.gitignore b/.gitignore index 11908c9..6ea46be 100644 --- a/.gitignore +++ b/.gitignore @@ -107,3 +107,4 @@ _UpgradeReport_Files/ Backup*/ UpgradeLog*.XML .nuget/NuGet.exe +.vs/ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index f6ab2fa..0000000 --- a/.travis.yml +++ /dev/null @@ -1,13 +0,0 @@ -language: csharp -solution: AustinHarris.JsonRpc.sln -mono: - - latest - - 3.12.0 - - 3.10.0 - - 3.8.0 -install: - - nuget restore AustinHarris.JsonRpc.sln - - nuget install NUnit.Runners -Version 2.6.4 -OutputDirectory testrunner -script: - - xbuild /p:Configuration=Release AustinHarris.JsonRpc.sln - - mono ./testrunner/NUnit.Runners.2.6.4/tools/nunit-console.exe ./AustinHarris.JsonRpcTestN/bin/Release/AustinHarris.JsonRpcTestN.dll \ No newline at end of file diff --git a/AustinHarris.JsonRpc.AspNet/AustinHarris.JsonRpc.AspNet.csproj b/AustinHarris.JsonRpc.AspNet/AustinHarris.JsonRpc.AspNet.csproj index 134986f..3470e2c 100644 --- a/AustinHarris.JsonRpc.AspNet/AustinHarris.JsonRpc.AspNet.csproj +++ b/AustinHarris.JsonRpc.AspNet/AustinHarris.JsonRpc.AspNet.csproj @@ -36,8 +36,8 @@ 4 - - ..\packages\Newtonsoft.Json.9.0.1\lib\net40\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.12.0.3\lib\net40\Newtonsoft.Json.dll True @@ -47,6 +47,7 @@ + diff --git a/AustinHarris.JsonRpc.AspNet/JsonRpcHandler.cs b/AustinHarris.JsonRpc.AspNet/JsonRpcHandler.cs index 7f2cf04..6a38c39 100644 --- a/AustinHarris.JsonRpc.AspNet/JsonRpcHandler.cs +++ b/AustinHarris.JsonRpc.AspNet/JsonRpcHandler.cs @@ -1,135 +1,16 @@ -using System; -using System.IO; -using System.IO.Compression; -using System.Text; -using System.Web; +using AustinHarris.JsonRpc.AspNet; namespace AustinHarris.JsonRpc.Handlers.AspNet { - public class JsonRpcHandler : IHttpAsyncHandler + /// + /// Used default SessionId + /// For routing use JsonRpcHandlerBase + /// + public class JsonRpcHandler : JsonRpcHandlerBase { - #region Fields - /// - /// UTF8 Encoding without BOM. - /// - static Encoding UTF8Encoding = new UTF8Encoding(false); - #endregion - - #region IHttpHandler Members - - public bool IsReusable - { - get { return true; } - } - - public void ProcessRequest(HttpContext context) - { - // not used - } - - #endregion - - #region IHttpAsyncHandler Members - - /// - /// Initiates an asynchronous call to the HTTP handler. - /// - /// An object that provides references to intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests. - /// The to call when the asynchronous method call is complete. If is null, the delegate is not called. - /// Any extra data needed to process the request. - /// - /// An that contains information about the status of the process. - /// - public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) - { - var async = new JsonRpcStateAsync(cb, context); - async.JsonRpc = GetJsonRpcString(context.Request); - JsonRpcProcessor.Process(async,context.Request); - return async; - } - - private static string GetJsonRpcString(System.Web.HttpRequest request) + protected override string GetSessionId() { - string json = string.Empty; - if (request.RequestType == "GET") - { - json = request.Params["jsonrpc"] ?? string.Empty; - } - else if (request.RequestType == "POST") - { - if (request.ContentType == "application/x-www-form-urlencoded") - { - json = request.Params["jsonrpc"] ?? string.Empty; - } - else - { - json = new StreamReader(request.InputStream).ReadToEnd(); - } - } - return json; + return Handler.DefaultSessionId(); } - - /// - /// Provides an asynchronous process End method when the process ends. - /// - /// An that contains information about the status of the process. - public void EndProcessRequest(IAsyncResult result) - { - var state = result as JsonRpcStateAsync; - if (state != null) - { - var r = state.Result; - var callback = ((HttpContext)state.AsyncState).Request.Params["callback"]; - if (!string.IsNullOrWhiteSpace(callback)) - { - r = string.Format("{0}({1})", callback, r); - } - - // try to compress the response data. - // fix me: compression filters in IHttpModule always failed for IHttpAsyncHandler - CompressResponseIfPossible(((HttpContext)state.AsyncState).Request, ((HttpContext)state.AsyncState).Response, r, UTF8Encoding); - } - } - - #endregion - - #region Utility methods - - /// - /// Transfer the result data compressed when the client accepts gzip. - /// - /// A HttpRequest object that represents the HTTP request. - /// A HttpResponse object that represents the HTTP response to be sent to the client. - /// The string data to be sent to the client. - /// The Encoding to be used to encode as the result. - static void CompressResponseIfPossible(HttpRequest request, HttpResponse response, String result, Encoding encoding) - { - string AcceptEncoding = request.Headers["Accept-Encoding"]; - if (AcceptEncoding != null && AcceptEncoding.Contains("gzip")) - { - //response.Headers.Remove("Content-Encoding"); - response.AddHeader("Content-Encoding", "gzip"); - - using (var gstream = new GZipStream(response.OutputStream, CompressionMode.Compress)) - using (var writer = new StreamWriter(gstream, encoding)) - { - writer.Write(result); - writer.Flush(); - } - } - else - { - using (StreamWriter writer = new StreamWriter(response.OutputStream, encoding)) - { - writer.Write(result); - writer.Flush(); - } - } - - response.End(); - } - - - #endregion } -} +} \ No newline at end of file diff --git a/AustinHarris.JsonRpc.AspNet/JsonRpcHandlerBase.cs b/AustinHarris.JsonRpc.AspNet/JsonRpcHandlerBase.cs new file mode 100644 index 0000000..8d61777 --- /dev/null +++ b/AustinHarris.JsonRpc.AspNet/JsonRpcHandlerBase.cs @@ -0,0 +1,138 @@ +using System; +using System.IO; +using System.IO.Compression; +using System.Text; +using System.Web; + +namespace AustinHarris.JsonRpc.AspNet +{ + public abstract class JsonRpcHandlerBase : IHttpAsyncHandler + { + #region Fields + /// + /// UTF8 Encoding without BOM. + /// + private static readonly Encoding Utf8Encoding = new UTF8Encoding(false); + + protected abstract string GetSessionId(); + #endregion + + #region IHttpHandler Members + + public bool IsReusable + { + get { return true; } + } + + public void ProcessRequest(HttpContext context) + { + // not used + } + + #endregion + + #region IHttpAsyncHandler Members + + /// + /// Initiates an asynchronous call to the HTTP handler. + /// + /// An object that provides references to intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests. + /// The to call when the asynchronous method call is complete. If is null, the delegate is not called. + /// Any extra data needed to process the request. + /// + /// An that contains information about the status of the process. + /// + public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData) + { + var async = new JsonRpcStateAsync(cb, context); + async.JsonRpc = GetJsonRpcString(context.Request); + JsonRpcProcessor.Process(GetSessionId(),async, context.Request); + return async; + } + + private static string GetJsonRpcString(System.Web.HttpRequest request) + { + string json = string.Empty; + if (request.RequestType == "GET") + { + json = request.Params["jsonrpc"] ?? string.Empty; + } + else if (request.RequestType == "POST") + { + if (request.ContentType == "application/x-www-form-urlencoded") + { + json = request.Params["jsonrpc"] ?? string.Empty; + } + else + { + json = new StreamReader(request.InputStream).ReadToEnd(); + } + } + return json; + } + + /// + /// Provides an asynchronous process End method when the process ends. + /// + /// An that contains information about the status of the process. + public void EndProcessRequest(IAsyncResult result) + { + var state = result as JsonRpcStateAsync; + if (state == null) return; + + var stateResult = state.Result; + var callback = ((HttpContext)state.AsyncState).Request.Params["callback"]; + if (!string.IsNullOrWhiteSpace(callback)) + { + stateResult = string.Format("{0}({1})", callback, stateResult); + } + + // try to compress the response data. + // fix me: compression filters in IHttpModule always failed for IHttpAsyncHandler + CompressResponseIfPossible(((HttpContext)state.AsyncState).Request, ((HttpContext)state.AsyncState).Response, stateResult, Utf8Encoding); + } + + #endregion + + #region Utility methods + + /// + /// Transfer the result data compressed when the client accepts gzip. + /// + /// A HttpRequest object that represents the HTTP request. + /// A HttpResponse object that represents the HTTP response to be sent to the client. + /// The string data to be sent to the client. + /// The Encoding to be used to encode as the result. + static void CompressResponseIfPossible(HttpRequest request, HttpResponse response, String result, Encoding encoding) + { + response.ContentType = "application/json-rpc"; + string acceptEncoding = request.Headers["Accept-Encoding"]; + if (acceptEncoding != null && acceptEncoding.Contains("gzip")) + { + //response.Headers.Remove("Content-Encoding"); + response.AddHeader("Content-Encoding", "gzip"); + + using (var gstream = new GZipStream(response.OutputStream, CompressionMode.Compress)) + using (var writer = new StreamWriter(gstream, encoding)) + { + writer.Write(result); + writer.Flush(); + } + } + else + { + using (StreamWriter writer = new StreamWriter(response.OutputStream, encoding)) + { + writer.Write(result); + writer.Flush(); + } + } + + response.End(); + } + + + #endregion + + } +} diff --git a/AustinHarris.JsonRpc.AspNet/packages.config b/AustinHarris.JsonRpc.AspNet/packages.config index 7c276ed..0fa4e01 100644 --- a/AustinHarris.JsonRpc.AspNet/packages.config +++ b/AustinHarris.JsonRpc.AspNet/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/AustinHarris.JsonRpc.sln b/AustinHarris.JsonRpc.sln index 858a102..6cf1c28 100644 --- a/AustinHarris.JsonRpc.sln +++ b/AustinHarris.JsonRpc.sln @@ -4,17 +4,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 12.0.40629.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BBFBBA8A-2F75-422C-ACCD-D05A6EF7244C}" - ProjectSection(SolutionItems) = preProject - AustinHarris.JsonRpc.vsmdi = AustinHarris.JsonRpc.vsmdi - Local.testsettings = Local.testsettings - Performance1.psess = Performance1.psess - TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings - EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AustinHarris.JsonRpc", "Json-Rpc\AustinHarris.JsonRpc.csproj", "{24FC1A2A-0BC3-43A7-9BFE-B628C2C4A307}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AustinHarris.JsonRpc.AspNet", "AustinHarris.JsonRpc.AspNet\AustinHarris.JsonRpc.AspNet.csproj", "{FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AustinHarris.JsonRpcTestN", "AustinHarris.JsonRpcTestN\AustinHarris.JsonRpcTestN.csproj", "{8569B076-5A8B-4D6A-B75D-EF75A390AA5F}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestServer_Console", "TestServer_Console\TestServer_Console.csproj", "{31AE59FC-B6F6-4AC7-A7B9-1E07630AE42B}" @@ -45,18 +37,6 @@ Global {24FC1A2A-0BC3-43A7-9BFE-B628C2C4A307}.Release|Mixed Platforms.Build.0 = Release|x86 {24FC1A2A-0BC3-43A7-9BFE-B628C2C4A307}.Release|x86.ActiveCfg = Release|x86 {24FC1A2A-0BC3-43A7-9BFE-B628C2C4A307}.Release|x86.Build.0 = Release|x86 - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Debug|ARM.ActiveCfg = Debug|Any CPU - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Debug|x86.ActiveCfg = Debug|Any CPU - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Release|Any CPU.Build.0 = Release|Any CPU - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Release|ARM.ActiveCfg = Release|Any CPU - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Release|Mixed Platforms.Build.0 = Release|Any CPU - {FFFDEBBC-93F5-4A22-9EC5-D86A4A792DBB}.Release|x86.ActiveCfg = Release|Any CPU {8569B076-5A8B-4D6A-B75D-EF75A390AA5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8569B076-5A8B-4D6A-B75D-EF75A390AA5F}.Debug|Any CPU.Build.0 = Debug|Any CPU {8569B076-5A8B-4D6A-B75D-EF75A390AA5F}.Debug|ARM.ActiveCfg = Debug|Any CPU diff --git a/AustinHarris.JsonRpcTestN/AustinHarris.JsonRpcTestN.csproj b/AustinHarris.JsonRpcTestN/AustinHarris.JsonRpcTestN.csproj index fe8c6d8..c5db038 100644 --- a/AustinHarris.JsonRpcTestN/AustinHarris.JsonRpcTestN.csproj +++ b/AustinHarris.JsonRpcTestN/AustinHarris.JsonRpcTestN.csproj @@ -1,69 +1,21 @@ - - + + - Debug - AnyCPU - {8569B076-5A8B-4D6A-B75D-EF75A390AA5F} - Library - AustinHarris.JsonRpcTestN - AustinHarris.JsonRpcTestN - v4.5 - ..\ - true + Austin Harris + netcoreapp3.0;netcoreapp3.1 - - true - full - false - bin\Debug - DEBUG; - prompt - 4 - false - - - full - true - bin\Release - prompt - 4 - false - - - - ..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll - True - - - ..\packages\NUnit.2.6.4\lib\nunit.framework.dll - True - - - - - - - - - - - Designer - - + + - - {24FC1A2A-0BC3-43A7-9BFE-B628C2C4A307} - AustinHarris.JsonRpc - + + + + + + - + - - - - This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - + \ No newline at end of file diff --git a/AustinHarris.JsonRpcTestN/Test.cs b/AustinHarris.JsonRpcTestN/Test.cs index 13d0ee7..ed46543 100644 --- a/AustinHarris.JsonRpcTestN/Test.cs +++ b/AustinHarris.JsonRpcTestN/Test.cs @@ -79,14 +79,14 @@ public void TestCanCreateAndRemoveSession() var actual1 = JObject.Parse(result.Result); var expected1 = JObject.Parse(expectedResult); - Assert.AreEqual(expected1, actual1); + Assert.IsTrue(JToken.DeepEquals(expected1, actual1)); h.Destroy(); var result2 = JsonRpcProcessor.Process("this one", request); result2.Wait(); - Assert.AreEqual(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result)); + Assert.IsTrue(JToken.DeepEquals(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result))); } [Test()] @@ -113,40 +113,17 @@ public void NullableDateTimeToNullableDateTime() Assert.AreEqual(expectedDate, acutalDate); } - [Test()] - public void NullableFloatToNullableFloat() - { - string request = @"{method:'NullableFloatToNullableFloat',params:[1.2345],id:1}"; - string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":1.2345,\"id\":1}"; - var result = JsonRpcProcessor.Process(request); - result.Wait(); - Assert.AreEqual(result.Result, expectedResult); - Assert.AreEqual(expectedResult, result.Result); - } - - [Test()] - public void NullableFloatToNullableFloat3() - { - string request = @"{method:'NullableFloatToNullableFloat',params:[3.14159],id:1}"; - string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":3.14159,\"id\":1}"; - var result = JsonRpcProcessor.Process(request); - result.Wait(); - Assert.AreEqual(result.Result, expectedResult); - Assert.AreEqual(expectedResult, result.Result); - } - - - [Test()] - public void NullableFloatToNullableFloat2() + [TestCase(@"{method:'NullableFloatToNullableFloat',params:[1.2345],id:1}", ExpectedResult = "{\"jsonrpc\":\"2.0\",\"result\":1.2345,\"id\":1}")] + [TestCase(@"{method:'NullableFloatToNullableFloat',params:[3.14159],id:1}", ExpectedResult = "{\"jsonrpc\":\"2.0\",\"result\":3.14159,\"id\":1}")] + [TestCase(@"{method:'NullableFloatToNullableFloat',params:[null],id:1}", ExpectedResult = "{\"jsonrpc\":\"2.0\",\"result\":null,\"id\":1}")] + public string NullableFloatToNullableFloat(string request) { - string request = @"{method:'NullableFloatToNullableFloat',params:[null],id:1}"; - string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":null,\"id\":1}"; var result = JsonRpcProcessor.Process(request); result.Wait(); - Assert.AreEqual(result.Result, expectedResult); - Assert.AreEqual(expectedResult, result.Result); + return result.Result; } - + + [Test()] public void DecimalToNullableDecimal() { @@ -196,7 +173,7 @@ public void StringToRefException() string expectedResult = "{\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"refException worked\",\"code\":-1,\"data\":null},\"id\":1}"; var result = JsonRpcProcessor.Process(request); result.Wait(); - Assert.AreEqual(JObject.Parse(expectedResult), JObject.Parse(result.Result)); + Assert.IsTrue(JToken.DeepEquals(JObject.Parse(expectedResult), JObject.Parse(result.Result))); } [Test()] @@ -1402,6 +1379,18 @@ public void TestOptionalParametersBoolsAndStrings() Assert.IsFalse(result.Result.Contains("error")); Assert.AreEqual(expectedResult, result.Result); } + + [TestCase("{method:\"TestDifferentOptionalParameters\",params:{location:\"loc1\", uid:\"abc123\", wavelengths: [0.0], traces: [0.0]},id:1}", ExpectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"this is the requested measurement\",\"id\":1}")] + [TestCase("{method:\"TestDifferentOptionalParameters\",params:{uid:\"abc123\", wavelengths: [0.0], traces: [0.0]},id:1}", ExpectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"this is the requested measurement\",\"id\":1}")] + [TestCase("{method:\"TestDifferentOptionalParameters\",params:{location:\"loc1\", uid:\"abc123\", traces: [0.0]},id:1}", ExpectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"this is the requested measurement\",\"id\":1}")] + [TestCase("{method:\"TestDifferentOptionalParameters\",params:{location:\"loc1\", uid:\"abc123\", wavelengths: [0.0]},id:1}", ExpectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"this is the requested measurement\",\"id\":1}")] + [TestCase("{method:\"TestDifferentOptionalParameters\",params:{uid:\"abc123\", wavelengths: [0.0]},id:1}", ExpectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"this is the requested measurement\",\"id\":1}")] + public string TestDifferentOptionalParametersNamedWorking(string request) + { + var result = JsonRpcProcessor.Process(request); + result.Wait(); + return result.Result; + } [Test()] public void TestBatchResultWrongRequests() @@ -1604,7 +1593,7 @@ public void TestPreProcessOnSession() var actual1 = JObject.Parse(result.Result); var expected1 = JObject.Parse(expectedResult); - Assert.AreEqual(expected1, actual1); + Assert.IsTrue(JToken.DeepEquals(expected1, actual1)); Assert.AreEqual(1, preHandler.run); h.Destroy(); @@ -1613,7 +1602,7 @@ public void TestPreProcessOnSession() result2.Wait(); Assert.AreEqual(1, preHandler.run); - Assert.AreEqual(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result)); + Assert.IsTrue(JToken.DeepEquals(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result))); } class PostProcessHandlerLocal @@ -1844,7 +1833,7 @@ public void TestPostProcessOnSession() var actual1 = JObject.Parse(result.Result); var expected1 = JObject.Parse(expectedResult); - Assert.AreEqual(expected1, actual1); + Assert.IsTrue(JToken.DeepEquals(expected1, actual1)); Assert.AreEqual(1, postHandler.run); h.Destroy(); @@ -1853,7 +1842,7 @@ public void TestPostProcessOnSession() result2.Wait(); Assert.AreEqual(1, postHandler.run); - Assert.AreEqual(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result)); + Assert.IsTrue(JToken.DeepEquals(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result))); } [Test()] @@ -1866,6 +1855,16 @@ public void TestExtraParameters() Assert.IsTrue(result.Result.Contains("\"code\":-32602")); } + [Test()] + public void TestExtraPositionalParameters() + { + string request = @"{method:'ReturnsDateTime',params:[1,2,'mytext'],id:1}"; + var result = JsonRpcProcessor.Process(request); + result.Wait(); + Assert.IsTrue(result.Result.Contains("error")); + Assert.IsTrue(result.Result.Contains("\"code\":-32602")); + } + [Test()] public void TestCustomParameterName() { @@ -1902,6 +1901,26 @@ public void TestNestedReturnType() Assert.AreEqual(expected, result.Result); } + [Test()] + public void TestWrongParamType() + { + string request = @"{method:'TestOptionalParamdouble',params:{input:'mytext'},id:1}"; + var result = JsonRpcProcessor.Process(request); + result.Wait(); + Assert.IsTrue(result.Result.Contains("error")); + Assert.IsTrue(result.Result.Contains("\"code\":-32603")); + } + + [Test()] + public void TestWrongIdType() + { + string request = @"{method:'TestOptionalParamdouble',params:{input:5},id:{what:4,that:3}}"; + var result = JsonRpcProcessor.Process(request); + result.Wait(); + Assert.IsTrue(result.Result.Contains("error")); + Assert.IsTrue(result.Result.Contains("\"code\":-32600")); + } + private static void AssertJsonAreEqual(string expectedJson, string actualJson) { Newtonsoft.Json.Linq.JObject expected = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(expectedJson); diff --git a/AustinHarris.JsonRpcTestN/packages.config b/AustinHarris.JsonRpcTestN/packages.config deleted file mode 100644 index 3632280..0000000 --- a/AustinHarris.JsonRpcTestN/packages.config +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/AustinHarris.JsonRpcTestN/service.cs b/AustinHarris.JsonRpcTestN/service.cs index 623a0e8..b34d957 100644 --- a/AustinHarris.JsonRpcTestN/service.cs +++ b/AustinHarris.JsonRpcTestN/service.cs @@ -389,6 +389,12 @@ public TreeNode TestNestedReturnType() } }; } + + [JsonRpcMethod] + private string TestDifferentOptionalParameters(string uid, string location = null, List traces = null, List wavelengths = null) + { + return "this is the requested measurement"; + } } } diff --git a/Json-Rpc/AustinHarris.JsonRpc.csproj b/Json-Rpc/AustinHarris.JsonRpc.csproj index 9d97aeb..f6a680d 100644 --- a/Json-Rpc/AustinHarris.JsonRpc.csproj +++ b/Json-Rpc/AustinHarris.JsonRpc.csproj @@ -1,100 +1,27 @@ - - - - Debug - x86 - {24FC1A2A-0BC3-43A7-9BFE-B628C2C4A307} - Library - Properties - AustinHarris.JsonRpc - AustinHarris.JsonRpc - - - 512 - SAK - SAK - SAK - SAK - ..\..\CoiniumServ\build\ - true - v4.0 - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - false - - - AnyCPU - pdbonly - true - bin\Release\ - - - prompt - 4 - false - - - AnyCPU - bin\Debug\ - TRACE;DEBUG - 4 - false - - - AnyCPU - bin\Release\ - 4 - true - false - - - - - - - - - - - - - - - - - - - - - ..\packages\Newtonsoft.Json.9.0.1\lib\net40\Newtonsoft.Json.dll - True - - - - - - - - - + + - This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + Austin Harris + Json-Rpc.Net Core + Core functionality for JsonRpc.Net + 1.2.3 + $(VersionSuffix) + Austin Harris + https://github.com/Astn/JSON-RPC.NET + https://raw.githubusercontent.com/Astn/JSON-RPC.NET/master/LICENSE + + Improves support for optional parameters - @HoMS1987 https://github.com/HoMS1987 + Fixes protocol validation of the ID property - @pedrolcl https://github.com/pedrolcl + DotNet Core support - @astn https://github.com/astn + + netstandard2.0;netstandard2.1;netcoreapp3.1 + true + - - - + + + + + + \ No newline at end of file diff --git a/Json-Rpc/AustinHarris.JsonRpc.nuspec b/Json-Rpc/AustinHarris.JsonRpc.nuspec index 0922df6..ad36f62 100644 --- a/Json-Rpc/AustinHarris.JsonRpc.nuspec +++ b/Json-Rpc/AustinHarris.JsonRpc.nuspec @@ -12,7 +12,7 @@ false The fastest .Net JSON RPC Server JSON-RPC.Net is a high performance Json-Rpc 2.0 server, leveraging the popular JSON.NET library. Easily create a JSON RPC server for your Angular javascript apps, also supports sockets and pipes, oh my! - Named Parameters via attributes -rommar. Fix nuget dependency requirements -jbreens. Cleaner service registration -xmedeko + Optional JsonSerializer Settings - @hovi. ProcessSync is now public - @astn en-US fast json rpc server socket javascript json-rpc.net json-rpc jsonrpc json.net web services webapi service angular server angularjs diff --git a/Json-Rpc/Handler.cs b/Json-Rpc/Handler.cs index c3e2e46..095bacb 100644 --- a/Json-Rpc/Handler.cs +++ b/Json-Rpc/Handler.cs @@ -14,13 +14,14 @@ public class Handler { #region Members - + private const string Name_of_JSONRPCEXCEPTION = "JsonRpcException&"; private static int _sessionHandlerMasterVersion = 1; [ThreadStatic] + private static Dictionary _sessionHandlersLocal; + [ThreadStatic] private static int _sessionHandlerLocalVersion = 0; private static ConcurrentDictionary _sessionHandlersMaster; - [ThreadStatic] - private static Dictionary _sessionHandlersLocal; + private static volatile string _defaultSessionId; #endregion @@ -212,13 +213,10 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null) SMDService metadata = null; Delegate handle = null; - var haveMetadata = this.MetaData.Services.TryGetValue(Rpc.Method, out metadata); - if (haveMetadata) + if (this.MetaData.Services.TryGetValue(Rpc.Method, out metadata)) { handle = metadata.dele; - } - - if (haveMetadata == false || metadata == null) + } else if (metadata == null) { JsonResponse response = new JsonResponse() { @@ -229,54 +227,54 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null) return PostProcess(Rpc, response, RpcContext); } - bool isJObject = Rpc.Params is Newtonsoft.Json.Linq.JObject; - bool isJArray = Rpc.Params is Newtonsoft.Json.Linq.JArray; object[] parameters = null; bool expectsRefException = false; var metaDataParamCount = metadata.parameters.Count(x => x != null); - var getCount = Rpc.Params as ICollection; + var loopCt = 0; - + var getCount = Rpc.Params as ICollection; if (getCount != null) { loopCt = getCount.Count; } var paramCount = loopCt; - if (paramCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount - 1].ObjectType.Name.Contains(typeof(JsonRpcException).Name)) + if (paramCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount - 1].ObjectType.Name.Equals(Name_of_JSONRPCEXCEPTION)) { paramCount++; expectsRefException = true; } parameters = new object[paramCount]; - if (isJArray) + if (Rpc.Params is Newtonsoft.Json.Linq.JArray) { var jarr = ((Newtonsoft.Json.Linq.JArray)Rpc.Params); - //var loopCt = jarr.Count; - //var pCount = loopCt; - //if (pCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount].GetType() == typeof(JsonRpcException)) - // pCount++; - //parameters = new object[pCount]; - for (int i = 0; i < loopCt; i++) + for (int i = 0; i < loopCt && i < metadata.parameters.Length; i++) { parameters[i] = CleanUpParameter(jarr[i], metadata.parameters[i]); } } - else if (isJObject) + else if (Rpc.Params is Newtonsoft.Json.Linq.JObject) { - var jo = Rpc.Params as Newtonsoft.Json.Linq.JObject; - //var loopCt = jo.Count; - //var pCount = loopCt; - //if (pCount == metaDataParamCount - 1 && metadata.parameters[metaDataParamCount].GetType() == typeof(JsonRpcException)) - // pCount++; - //parameters = new object[pCount]; - var asDict = jo as IDictionary; + var asDict = Rpc.Params as IDictionary; for (int i = 0; i < loopCt && i < metadata.parameters.Length; i++) { - if (asDict.ContainsKey(metadata.parameters[i].Name) == false) + if (asDict.ContainsKey(metadata.parameters[i].Name) == true) { + parameters[i] = CleanUpParameter(asDict[metadata.parameters[i].Name], metadata.parameters[i]); + continue; + } + else + { + var foundDefault = metadata.defaultValues + .FirstOrDefault(defaul => defaul.Name == metadata.parameters[i].Name); + if (foundDefault != null) + { + parameters[i] = foundDefault.Value; + continue; + } + JsonResponse response = new JsonResponse() { Error = ProcessException(Rpc, @@ -289,7 +287,6 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null) }; return PostProcess(Rpc, response, RpcContext); } - parameters[i] = CleanUpParameter(jo[metadata.parameters[i].Name], metadata.parameters[i]); } } @@ -348,17 +345,20 @@ public JsonResponse Handle(JsonRequest Rpc, Object RpcContext = null) var last = parameters.LastOrDefault(); var contextException = RpcGetAndRemoveRpcException(); + JsonResponse response = null; if (contextException != null) { - JsonResponse response = new JsonResponse() { Error = ProcessException(Rpc, contextException), Id = Rpc.Id }; - return PostProcess(Rpc, response, RpcContext); + response = new JsonResponse() { Error = ProcessException(Rpc, contextException), Id = Rpc.Id }; } - if (expectsRefException && last != null && last is JsonRpcException) + else if (expectsRefException && last != null && last is JsonRpcException) { - JsonResponse response = new JsonResponse() { Error = ProcessException(Rpc, last as JsonRpcException), Id = Rpc.Id }; - return PostProcess(Rpc, response, RpcContext); + response = new JsonResponse() { Error = ProcessException(Rpc, last as JsonRpcException), Id = Rpc.Id }; + } + else + { + response = new JsonResponse() { Result = results }; } - return PostProcess(Rpc, new JsonResponse() { Result = results }, RpcContext); + return PostProcess(Rpc, response, RpcContext); } catch (Exception ex) { @@ -431,42 +431,42 @@ internal void SetParseErrorHandler(Func { [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "jsonrpc")] - public string JsonRpc { get { return "2.0"; } } + public string JsonRpc { get; set; } = "2.0"; [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "result")] public T Result { get; set; } diff --git a/Json-Rpc/JsonRpcProcessor.cs b/Json-Rpc/JsonRpcProcessor.cs index 0328b95..5020e88 100644 --- a/Json-Rpc/JsonRpcProcessor.cs +++ b/Json-Rpc/JsonRpcProcessor.cs @@ -13,116 +13,168 @@ namespace AustinHarris.JsonRpc { public static class JsonRpcProcessor { - public static void Process(JsonRpcStateAsync async, object context = null) + public static void Process(JsonRpcStateAsync async, object context = null, + JsonSerializerSettings settings = null) { - Process(Handler.DefaultSessionId(), async, context); + Process(Handler.DefaultSessionId(), async, context, settings); } - public static void Process(string sessionId, JsonRpcStateAsync async, object context = null) + public static void Process(string sessionId, JsonRpcStateAsync async, object context = null, + JsonSerializerSettings settings = null) { - Process(sessionId, async.JsonRpc, context) + Process(sessionId, async.JsonRpc, context, settings) .ContinueWith(t => - { - async.Result = t.Result; - async.SetCompleted(); - }); + { + async.Result = t.Result; + async.SetCompleted(); + }); } - public static Task Process(string jsonRpc, object context = null) + + public static Task Process(string jsonRpc, object context = null, + JsonSerializerSettings settings = null) { - return Process(Handler.DefaultSessionId(), jsonRpc, context); + return Process(Handler.DefaultSessionId(), jsonRpc, context, settings); } - public static Task Process(string sessionId, string jsonRpc, object context = null) - { + + public static Task Process(string sessionId, string jsonRpc, object context = null, + JsonSerializerSettings settings = null) + { return Task.Factory.StartNew((_) => { - var tuple = (Tuple)_; - return ProcessInternal(tuple.Item1, tuple.Item2, tuple.Item3); - }, new Tuple(sessionId, jsonRpc, context)); + var tuple = (Tuple)_; + return ProcessSync(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4); + }, new Tuple(sessionId, jsonRpc, context, settings)); } - private static string ProcessInternal(string sessionId, string jsonRpc, object jsonRpcContext) + public static string ProcessSync(string sessionId, string jsonRpc, object jsonRpcContext, + JsonSerializerSettings settings = null) { var handler = Handler.GetSessionHandler(sessionId); + + JsonRequest[] batch = null; try { - Tuple[] batch = null; if (isSingleRpc(jsonRpc)) { - batch = new[] { Tuple.Create(JsonConvert.DeserializeObject(jsonRpc), new JsonResponse()) }; + var foo = JsonConvert.DeserializeObject(jsonRpc, settings); + batch = new[] { foo }; } else { - batch = JsonConvert.DeserializeObject(jsonRpc) - .Select(request => new Tuple(request, new JsonResponse())) - .ToArray(); + batch = JsonConvert.DeserializeObject(jsonRpc, settings); } + } + catch (Exception ex) + { + return Newtonsoft.Json.JsonConvert.SerializeObject(new JsonResponse + { + Error = handler.ProcessParseException(jsonRpc, new JsonRpcException(-32700, "Parse error", ex)) + }, settings); + } - if (batch.Length == 0) + if (batch.Length == 0) + { + return Newtonsoft.Json.JsonConvert.SerializeObject(new JsonResponse { - return Newtonsoft.Json.JsonConvert.SerializeObject(new JsonResponse - { - Error = handler.ProcessParseException(jsonRpc, - new JsonRpcException(3200, "Invalid Request", "Batch of calls was empty.")) - }); - } + Error = handler.ProcessParseException(jsonRpc, + new JsonRpcException(3200, "Invalid Request", "Batch of calls was empty.")) + }, settings); + } - foreach (var tuple in batch) + var singleBatch = batch.Length == 1; + StringBuilder sbResult = null; + for (var i = 0; i < batch.Length; i++) + { + var jsonRequest = batch[i]; + var jsonResponse = new JsonResponse(); + + if (jsonRequest == null) + { + jsonResponse.Error = handler.ProcessParseException(jsonRpc, + new JsonRpcException(-32700, "Parse error", + "Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.")); + } + else if (jsonRequest.Method == null) + { + jsonResponse.Error = handler.ProcessParseException(jsonRpc, + new JsonRpcException(-32600, "Invalid Request", "Missing property 'method'")); + } + else if (!isSimpleValueType(jsonRequest.Id)) { - var jsonRequest = tuple.Item1; - var jsonResponse = tuple.Item2; + jsonResponse.Error = handler.ProcessParseException(jsonRpc, + new JsonRpcException(-32600, "Invalid Request", "Id property must be either null or string or integer.")); + } + else + { + jsonResponse.Id = jsonRequest.Id; + + var data = handler.Handle(jsonRequest, jsonRpcContext); - if (jsonRequest == null) + if (data == null) continue; + + jsonResponse.JsonRpc = data.JsonRpc; + jsonResponse.Error = data.Error; + jsonResponse.Result = data.Result; + + } + if (jsonResponse.Result == null && jsonResponse.Error == null) + { + // Per json rpc 2.0 spec + // result : This member is REQUIRED on success. + // This member MUST NOT exist if there was an error invoking the method. + // Either the result member or error member MUST be included, but both members MUST NOT be included. + jsonResponse.Result = new Newtonsoft.Json.Linq.JValue((Object)null); + } + // special case optimization for single Item batch + if (singleBatch && (jsonResponse.Id != null || jsonResponse.Error != null)) + { + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw); + writer.WriteStartObject(); + if (!string.IsNullOrEmpty(jsonResponse.JsonRpc)) + { + writer.WritePropertyName("jsonrpc"); writer.WriteValue(jsonResponse.JsonRpc); + } + if (jsonResponse.Error != null) { - jsonResponse.Error = handler.ProcessParseException(jsonRpc, - new JsonRpcException(-32700, "Parse error", - "Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text.")); + writer.WritePropertyName("error"); writer.WriteRawValue(JsonConvert.SerializeObject(jsonResponse.Error, settings)); } else { - jsonResponse.Id = jsonRequest.Id; - - if (jsonRequest.Method == null) - { - jsonResponse.Error = handler.ProcessParseException(jsonRpc, - new JsonRpcException(-32600, "Invalid Request", "Missing property 'method'")); - } - else - { - var data = handler.Handle(jsonRequest, jsonRpcContext); - - if (data == null) continue; - - jsonResponse.Error = data.Error; - jsonResponse.Result = data.Result; - } + writer.WritePropertyName("result"); writer.WriteRawValue(JsonConvert.SerializeObject(jsonResponse.Result, settings)); } - } + writer.WritePropertyName("id"); writer.WriteValue(jsonResponse.Id); + writer.WriteEndObject(); + return sw.ToString(); - var responses = new string[batch.Count(x => x.Item2.Id != null || x.Item2.Error != null)]; - var idx = 0; - foreach (var resp in batch.Where(x => x.Item2.Id != null || x.Item2.Error != null)) + //return JsonConvert.SerializeObject(jsonResponse); + } + else if (jsonResponse.Id == null && jsonResponse.Error == null) { - if (resp.Item2.Result == null && resp.Item2.Error == null) + // do nothing + sbResult = new StringBuilder(0); + } + else + { + // write out the response + if (i == 0) { - // Per json rpc 2.0 spec - // result : This member is REQUIRED on success. - // This member MUST NOT exist if there was an error invoking the method. - // Either the result member or error member MUST be included, but both members MUST NOT be included. - resp.Item2.Result = new Newtonsoft.Json.Linq.JValue((Object)null); + sbResult = new StringBuilder("["); } - responses[idx++] = JsonConvert.SerializeObject(resp.Item2); - } - return responses.Length == 0 ? string.Empty : responses.Length == 1 ? responses[0] : string.Format("[{0}]", string.Join(",", responses)); - } - catch (Exception ex) - { - return Newtonsoft.Json.JsonConvert.SerializeObject(new JsonResponse - { - Error = handler.ProcessParseException(jsonRpc, new JsonRpcException(-32700, "Parse error", ex)) - }); + sbResult.Append(JsonConvert.SerializeObject(jsonResponse, settings)); + if (i < batch.Length - 1) + { + sbResult.Append(','); + } + else if (i == batch.Length - 1) + { + sbResult.Append(']'); + } + } } + return sbResult.ToString(); } private static bool isSingleRpc(string json) @@ -134,5 +186,15 @@ private static bool isSingleRpc(string json) } return true; } + + private static bool isSimpleValueType(object property) + { + if (property == null) + return true; + return property.GetType() == typeof(System.String) || + property.GetType() == typeof(System.Int64) || + property.GetType() == typeof(System.Int32) || + property.GetType() == typeof(System.Int16); + } } } diff --git a/Json-Rpc/JsonRpcService.cs b/Json-Rpc/JsonRpcService.cs index 4644b24..d8ec961 100644 --- a/Json-Rpc/JsonRpcService.cs +++ b/Json-Rpc/JsonRpcService.cs @@ -1,21 +1,22 @@ namespace AustinHarris.JsonRpc { - using System; - using System.Collections.Generic; - using System.Linq; - using System.Reflection; - using AustinHarris.JsonRpc; - + /// + /// For routing use SessionId + /// public abstract class JsonRpcService { protected JsonRpcService() { - ServiceBinder.BindService(Handler.DefaultSessionId(), this); + ServiceBinder.BindService(Handler.DefaultSessionId(), this); } + /// + /// Routing by SessionId + /// + /// protected JsonRpcService(string sessionID) { ServiceBinder.BindService(sessionID, this); } } -} +} \ No newline at end of file diff --git a/Json-Rpc/Properties/AssemblyInfo.cs b/Json-Rpc/Properties/AssemblyInfo.cs deleted file mode 100644 index 802d0fd..0000000 --- a/Json-Rpc/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("Json-Rpc.Net Core")] -[assembly: AssemblyDescription("Core functionality for JsonRpc.Net")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("Austin Harris")] -[assembly: AssemblyProduct("Json-Rpc.Net Core")] -[assembly: AssemblyCopyright("Austin Harris")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("2f8036b2-223d-4b90-b6a9-fadddeb3ac0d")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -[assembly: AssemblyVersion("1.0.5.0")] -[assembly: AssemblyFileVersion("1.0.5.0")] diff --git a/Json-Rpc/packages.config b/Json-Rpc/packages.config deleted file mode 100644 index 7c276ed..0000000 --- a/Json-Rpc/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/README.md b/README.md index 2d4b940..7847e23 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,35 @@ json-rpc.net ============ -.Net [![astn-jsonrpc MyGet Build Status](https://www.myget.org/BuildSource/Badge/astn-jsonrpc?identifier=fbc64a4a-f9a7-4306-87ba-de0bb9d23cb7)](https://www.myget.org/feed/Activity/astn-jsonrpc) Mono [![Build Status](https://travis-ci.org/Astn/JSON-RPC.NET.svg?branch=master)](https://travis-ci.org/Astn/JSON-RPC.NET) +![Build Master](https://github.com/Astn/JSON-RPC.NET/workflows/Build%20Master/badge.svg) ![NuGet Badge](https://buildstats.info/nuget/AustinHarris.JsonRpc) JSON-RPC.Net is a high performance Json-Rpc 2.0 server, leveraging the popular JSON.NET library. Host in ASP.NET, also supports sockets and pipes, oh my! +## Performance + +These are results from running the TestServer_Console project. + +##### Xeon E-2176M @ 2.70GHz 64.0 GB (Date: Thu Apr 30 17:34:22 2020 -0600) + +``` +Starting benchmark +processed 50 rpc in 137ms for 364.96 rpc/sec +processed 100 rpc in 0ms for ∞ rpc/sec +processed 300 rpc in 1ms for 300,000.00 rpc/sec +processed 1,200 rpc in 7ms for 171,428.57 rpc/sec +processed 6,000 rpc in 26ms for 230,769.23 rpc/sec +processed 36,000 rpc in 166ms for 216,867.47 rpc/sec +processed 252,000 rpc in 1,121ms for 224,799.29 rpc/sec +Finished benchmark... +``` + +## Do you like this? + +[![https://www.buymeacoffee.com/Ekati](https://cdn.buymeacoffee.com/buttons/default-blue.png)](https://www.buymeacoffee.com/Ekati) + + ##### Requirements -* dotnet 4.0 or mono +* dotnet-standard (dotnet core | mono | .net framework) ##### License JSON-RPC.net is licensed under The MIT License (MIT), check the [LICENSE](https://github.com/CoiniumServ/JSON-RPC.NET/blob/master/LICENSE) file for details. @@ -29,9 +52,10 @@ To install JSON-RPC.NET AspNet, run the following command in the Package Manager PM> Install-Package AustinHarris.JsonRpc.AspNet ``` -##### Performance -Under ideal conditions > 120k rpc/sec (cpu i7-2600,console server, no IO bottleneck) + + + ##### Getting Started & Documentation diff --git a/TestServer_Console/Program.cs b/TestServer_Console/Program.cs index 20226b5..df3152f 100644 --- a/TestServer_Console/Program.cs +++ b/TestServer_Console/Program.cs @@ -5,6 +5,7 @@ using AustinHarris.JsonRpc; using System.Threading; using System.Diagnostics; +using System.Threading.Tasks; namespace TestServer_Console { @@ -47,38 +48,27 @@ private static void Benchmark() { Console.WriteLine("Starting benchmark"); - var cnt = 40; + var cnt = 50; var iterations = 7; for (int iteration = 1; iteration <= iterations; iteration++) { cnt *= iteration; ctr = 0; + Task[] tasks = new Task[cnt]; var sw = Stopwatch.StartNew(); - AutoResetEvent are = new AutoResetEvent(false); - - var fn = new Action>(_ => { - if(Interlocked.Increment(ref ctr) == cnt) - { - sw.Stop(); - Console.WriteLine("processed {0} rpc in {1}ms for {2} rpc/sec",cnt,sw.ElapsedMilliseconds, (double)cnt * 1000d / sw.ElapsedMilliseconds); - are.Set(); - } - }); var sessionid = Handler.DefaultSessionId(); for (int i = 0; i < cnt; i+=5) { - JsonRpcProcessor.Process(sessionid, "{'method':'add','params':[1,2],'id':1}").ContinueWith(fn); - - JsonRpcProcessor.Process(sessionid, "{'method':'addInt','params':[1,7],'id':2}").ContinueWith(fn); - - JsonRpcProcessor.Process(sessionid, "{'method':'NullableFloatToNullableFloat','params':[1.23],'id':3}").ContinueWith(fn); - - JsonRpcProcessor.Process(sessionid, "{'method':'Test2','params':[3.456],'id':4}").ContinueWith(fn); - - JsonRpcProcessor.Process(sessionid, "{'method':'StringMe','params':['Foo'],'id':5}").ContinueWith(fn); + tasks[i] = JsonRpcProcessor.Process(sessionid, "{'method':'add','params':[1,2],'id':1}"); + tasks[i+1] = JsonRpcProcessor.Process(sessionid, "{'method':'addInt','params':[1,7],'id':2}"); + tasks[i+2] = JsonRpcProcessor.Process(sessionid, "{'method':'NullableFloatToNullableFloat','params':[1.23],'id':3}"); + tasks[i+3] = JsonRpcProcessor.Process(sessionid, "{'method':'Test2','params':[3.456],'id':4}"); + tasks[i+4] = JsonRpcProcessor.Process(sessionid, "{'method':'StringMe','params':['Foo'],'id':5}"); } - are.WaitOne(); + Task.WaitAll(tasks); + sw.Stop(); + Console.WriteLine("processed {0:N0} rpc in \t {1:N0}ms for \t {2:N} rpc/sec", cnt, sw.ElapsedMilliseconds, (double)cnt * 1000d / sw.ElapsedMilliseconds); } diff --git a/TestServer_Console/Properties/AssemblyInfo.cs b/TestServer_Console/Properties/AssemblyInfo.cs deleted file mode 100644 index fdd1edf..0000000 --- a/TestServer_Console/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("TestServer_Console")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("TestServer_Console")] -[assembly: AssemblyCopyright("Copyright © 2012")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("5e7696bc-52cc-48f8-a575-40b17165ee20")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TestServer_Console/TestServer_Console.csproj b/TestServer_Console/TestServer_Console.csproj index be9574b..ba5f427 100644 --- a/TestServer_Console/TestServer_Console.csproj +++ b/TestServer_Console/TestServer_Console.csproj @@ -1,98 +1,24 @@ - - + + - Debug - x86 - {31AE59FC-B6F6-4AC7-A7B9-1E07630AE42B} + Austin Harris Exe - Properties - TestServer_Console - TestServer_Console - v4.0 - Client - 512 - SAK - SAK - SAK - SAK - ..\ - true + netcoreapp3.1 - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - x86 - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - AnyCPU - bin\Debug\ - 4 - false - - - AnyCPU - bin\Release\ - 4 - true - false - - - - ..\packages\Newtonsoft.Json.9.0.1\lib\net40\Newtonsoft.Json.dll - True - - - - - - - - - - ..\packages\Newtonsoft.Json.7.0.1\lib\net40\Newtonsoft.Json.dll - - + + - - - + + + - - {24FC1A2A-0BC3-43A7-9BFE-B628C2C4A307} - AustinHarris.JsonRpc - + + + - + - - - - - - This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - + \ No newline at end of file diff --git a/TestServer_Console/packages.config b/TestServer_Console/packages.config deleted file mode 100644 index e307e1c..0000000 --- a/TestServer_Console/packages.config +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file