This project is implemented in accordance with the. NET implementation of the JSON-RPC 2.0 specification, a lightweight, loosely coupled, remote invocation service framework that is implemented based on HTTP or TCP or local invocation, providing an efficient, non-intrusive way to build your own service platform that can be rapidly developed, Debug, publish, invoke services, keep lightweight and customizable. You can build a microservices or API for the. NET platform based on it.
Important Features
- Support for HTTP and local calls currently supported
- Powerful automatic description of service descriptions
- Supports RSA and custom authorization mechanisms
- Support for custom contexts
How to develop and implement services
1. Create a new or use the original Class Library project, such as OF.Microservices.DemoService, which does not require reference to an external dependency package of. JsonRpc.dll, and then set in the project properties, build property page > Output > Tick Xml documentation file so that the online document can be generated automatically.
2. The of of the created earlier. JSONRPC Project, create a service class of your own, and write a public method, write the corresponding comments, as follows:
namespaceof . microservices.demoservice{/// <summary> ///Demo Service 1/// </summary> Public classDemo1 {/// <summary> ///output two values for a parameter/// </summary> /// <param name= "param1" >parameter 1</param> /// <param name= "param2" >Parameter 2</param> /// <returns>returns information for parameter 1 and parameter 2</returns> Public stringMETHOD1 (stringPARAM1,stringparam2) { return$"param1: {param1}, param2: {param2}"; } }}
3. Place the project of the item in the. Microservices.host a Web site that is set to IIS. and configure the Jsonrpc.config file to configure the service implementation DLL assembly name that needs to be exposed under the Serviceassemblies node, an example of the Jsonrpc.config configuration file is as follows:
<?XML version= "1.0" encoding= "Utf-8"?><Jsonrpc> <serviceassemblies> <AddAssembly= "of". Microservices.demoservice "Domain= "Demo"Methodmode= "Allpublic"Subdomaintrimchars= "Service" /> </serviceassemblies></Jsonrpc>
4. Then use the browser to open the root address of the site, such as: http://localhost:60282 to view the service's documentation.
5. Address of service exposure: HTTP://LOCALHOST:60282/JSON.RPC
Calls to Client C #
For details, see the. Jsonrpc\of. The code in the Jsonrpc.clienttest\calldemotest.cs. Needs to be introduced on the client. Jsonrpc.client the assembly and configure the service address in App.config/web.config:
< Configuration > < appSettings > < key= "Jsonrpcserviceurl" value= "Http://localhost:60282/json.rpc" /> </appSettings>
The code that is called is very simple, does not need to define what interface, completely loose coupling way, Rpc.call corresponding parameter is method name and order parameter. Examples are as follows:
//Invoke Service, enter 2 parameters, return a stringvarRESULT1 = rpc.call<string>( "Demo.Demo1.Method1", "Jack","John");//Call service, no return valueRpc.call ("Demo.Demo1.Method2","Jack");//invokes the service, returning an anonymous dynamic objectvarresult = rpc.call<Dynamic>( "Demo.Demo1.Method3",//Json-rpc Method Name New{Name="Jack", Email="[email protected]", Mobilephone="12306" }); //the parameters of the JSON-RPC,Console.WriteLine ("result value, OrderNumber:"+result. ORDERID); Console.WriteLine ("Customer.username:"+result. Customer.name); Console.WriteLine ("Customer.email:"+result. Customer.email); Console.WriteLine ("Customer.mobilephone:"+result. Customer.mobilephone);//Exception HandlingTry{ varRESULT1 = rpc.call<string>( "Demo.Demo1.Method1", "Jack","John"); Console.WriteLine ("result Value:"+result1);}Catch(jsonrpcexception jsonrpcexception) {if(Jsonrpcexception.code = =32000) Console.WriteLine ("result Business excetion:"+jsonrpcexception.message); ElseConsole.WriteLine ("the invocation service has an exception, {0}, {1}", Jsonrpcexception.message, jsonrpcexception.data);}//calling parameters as named Object PropertiesstringRESULT2 = rpc.callwithdeclaredparams<string>( "Demo.Demo1.Method1", New{param2 ="John", param1 ="Jack"});
Browse JSON-RPC Help Documentation
Open the browser address: http://localhost:60282 After the document first page, you can filter
Detailed description of the entry method:
Enter the type description for the parameter or return value:
Java calls
Because of the uniform JSON-RPC HTTP protocol used, some three-party libraries are used, recommended: https://github.com/briandilley/jsonrpc4j
New jsonrpchttpclient ( new URL ("Http://example.com/UserService.json"New Object[] {"Bob", "The Builder"}, User. class);
Javascript calls
, so using some three-party libraries, recommended: HTTPS://GITHUB.COM/DATAGRAPH/JQUERY-JSONRPC
Example:
$.jsonrpc.request (' Method.name ', { params:params, function(result) { do something with the resulthere // It comes back as an RPC 2.0 compatible response OB Ject }, function(result) { // result is ' an ' RPC 2.0 Compatible Response object }});
ToDoList
- Introduction of Zookeeper as a choice of service discovery mechanism
- Support for distributed transaction support across service invocations
- Join the. NET Core version
- You can not use the IIS host
Of. Jsonrpc (. NET Lightweight service Framework)