Title: Usage of await operator in the JsonRpcMethod. · Issue #147 · Astn/JSON-RPC.NET · GitHub
Open Graph Title: Usage of await operator in the JsonRpcMethod. · Issue #147 · Astn/JSON-RPC.NET
X Title: Usage of await operator in the JsonRpcMethod. · Issue #147 · Astn/JSON-RPC.NET
Description: Hello. I was wondering if it is possible to use the await operator in [JsonRpcMethod] methods? Inside the [JsonRpcMethod] I would like to call asynchronous methods and return Task
Open Graph Description: Hello. I was wondering if it is possible to use the await operator in [JsonRpcMethod] methods? Inside the [JsonRpcMethod] I would like to call asynchronous methods and return Task
X Description: Hello. I was wondering if it is possible to use the await operator in [JsonRpcMethod] methods? Inside the [JsonRpcMethod] I would like to call asynchronous methods and return Task<T> from the...
Opengraph URL: https://github.com/Astn/JSON-RPC.NET/issues/147
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Usage of await operator in the JsonRpcMethod.","articleBody":"Hello. I was wondering if it is possible to use the await operator in `[JsonRpcMethod]` methods?\r\nInside the `[JsonRpcMethod]` I would like to call asynchronous methods and return `Task\u003cT\u003e` from the function. But this approach throws an exception. All I could do was call synchronous versions of the functions and return `Task.FromResult` from the function.\r\n\r\nSome examples:\r\n\r\n```\r\nusing AustinHarris.JsonRpc;\r\n\r\nobject[] services = new object[] { new ExampleService() };\r\n\r\nfor (string line = Console.ReadLine(); !string.IsNullOrEmpty(line); line = Console.ReadLine())\r\n{\r\n try\r\n {\r\n var response = await JsonRpcProcessor.Process(line);\r\n Console.WriteLine(response);\r\n }\r\n catch(Exception ex)\r\n {\r\n Console.WriteLine(ex.ToString());\r\n }\r\n\r\n}\r\npublic class ExampleService : JsonRpcService\r\n{\r\n [JsonRpcMethod]\r\n private async Task\u003cdouble\u003e DoSomethingAsync(double l, double r) // {'method':'DoSomethingAsync','params':[1.0,2.0],'id':1}\r\n {\r\n return await Worker.SummAsync(l, r);\r\n }\r\n}\r\npublic class Worker\r\n{\r\n public static async Task\u003cdouble\u003e SummAsync(double a, double b)\r\n {\r\n await Task.Delay(1000);\r\n return await Task.FromResult(a + b);\r\n }\r\n}\r\n```\r\nException:\r\n```\r\nNewtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'Task' with type 'System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1+AsyncStateMachineBox`1[System.Double,ExampleService+\u003cDoSomethingAsync\u003ed__0]'. Path 'StateMachine.\u003c\u003et__builder'.\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract\u0026 memberContract, Object\u0026 memberValue)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)\r\n at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, JsonSerializerSettings settings)\r\n at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)\r\n at AustinHarris.JsonRpc.JsonRpcProcessor.ProcessSync(String sessionId, String jsonRpc, Object jsonRpcContext, JsonSerializerSettings settings)\r\n at AustinHarris.JsonRpc.JsonRpcProcessor.\u003c\u003ec.\u003cProcess\u003eb__3_0(Object _)\r\n at System.Threading.Tasks.Task`1.InnerInvoke()\r\n at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)\r\n--- End of stack trace from previous location ---\r\n at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task\u0026 currentTaskSlot, Thread threadPoolThread)\r\n--- End of stack trace from previous location ---\r\n at Program.\u003cMain\u003e$(String[] args) in D:\\Work\\other\\JsonRpcNetAsync\\JsonRpcNetAsync\\Program.cs:line 9\r\n\r\n```\r\nWorking example:\r\n```\r\nusing AustinHarris.JsonRpc;\r\n\r\nobject[] services = new object[] { new ExampleService() };\r\n\r\nfor (string line = Console.ReadLine(); !string.IsNullOrEmpty(line); line = Console.ReadLine())\r\n{\r\n try\r\n {\r\n Task\u003cstring\u003e task1 = JsonRpcProcessor.Process(line);\r\n Task\u003cstring\u003e task2 = JsonRpcProcessor.Process(line);\r\n Task\u003cstring\u003e task3 = JsonRpcProcessor.Process(line);\r\n Task\u003cstring\u003e[] tasks = [task1, task2, task3];\r\n string[] result = await Task.WhenAll(tasks);\r\n foreach (string res in result) Console.WriteLine(res);\r\n }\r\n catch(Exception ex)\r\n {\r\n Console.WriteLine(ex.ToString());\r\n }\r\n\r\n}\r\npublic class ExampleService : JsonRpcService\r\n{\r\n [JsonRpcMethod]\r\n private Task\u003cdouble\u003e DoSomethingAsync(double l, double r) // {'method':'DoSomethingAsync','params':[1.0,2.0],'id':1}\r\n {\r\n return Task.FromResult(Worker.SummSync(l, r));\r\n }\r\n}\r\npublic class Worker\r\n{\r\n public static double SummSync(double a, double b)\r\n {\r\n Thread.Sleep(10000);\r\n return a + b;\r\n }\r\n}\r\n```\r\nIf I understand correctly, calling `JsonRpcProcessor.Process(line)` runs in a separate thread, so blocking synchronous methods do not block the calling thread. But in the future, I might want to call asynchronous methods, and I was wondering if it is possible to do so using your framework? Thanks a lot for your answer!","author":{"url":"https://github.com/posahok","@type":"Person","name":"posahok"},"datePublished":"2024-09-17T16:31:53.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/147/JSON-RPC.NET/issues/147"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6a9e6de9-ec89-ce39-efa5-44461592b3de |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BB6A:1DF886:2F100C5:3C19F88:69904442 |
| html-safe-nonce | 8374377814514b95ead73281bdf1cd05f83c80e93dea545e3655b41d28a66807 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQjZBOjFERjg4NjoyRjEwMEM1OjNDMTlGODg6Njk5MDQ0NDIiLCJ2aXNpdG9yX2lkIjoiMzg3OTMzMDUzOTMwNDQwNDAzNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 60bf080644e37dc860e24685ad0887d821ace30cf5d2c29b9f172718eac7b356 |
| hovercard-subject-tag | issue:2531595394 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/Astn/JSON-RPC.NET/147/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ce256320dc4ddb05ebca0749110519aa18863b20532bda8fe78efe301e4be95f/Astn/JSON-RPC.NET/issues/147 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ce256320dc4ddb05ebca0749110519aa18863b20532bda8fe78efe301e4be95f/Astn/JSON-RPC.NET/issues/147 |
| og:image:alt | Hello. I was wondering if it is possible to use the await operator in [JsonRpcMethod] methods? Inside the [JsonRpcMethod] I would like to call asynchronous methods and return Task |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | posahok |
| hostname | github.com |
| expected-hostname | github.com |
| None | 42c603b9d642c4a9065a51770f75e5e27132fef0e858607f5c9cb7e422831a7b |
| turbo-cache-control | no-preview |
| go-import | github.com/Astn/JSON-RPC.NET git https://github.com/Astn/JSON-RPC.NET.git |
| octolytics-dimension-user_id | 6857743 |
| octolytics-dimension-user_login | Astn |
| octolytics-dimension-repository_id | 17465376 |
| octolytics-dimension-repository_nwo | Astn/JSON-RPC.NET |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 17465376 |
| octolytics-dimension-repository_network_root_nwo | Astn/JSON-RPC.NET |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 3b33c5aedc9808f45bc5fcf0b1e4404cf749dac7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width