Skip to content

Getting Started (Console)

sorn6541 edited this page Jun 22, 2018 · 4 revisions

Using json-rpc.net within a console application;

Create a JsonRpcService

public class ExampleCalculatorService : JsonRpcService
    {
        [JsonRpcMethod]
        private double add(double l, double r)
        {
            return l+r;
        }
    }

Register that service before it will ever be used

namespace Example
{
    class ConsoleServer
    {
        static object[] services = new object[] {
           new ExampleCalculatorService()
        };
    }
}

Direct input from the console to the JsonRpc Processor and pass the output back to the console

  static void Main(string[] args)
        {
            var rpcResultHandler = new AsyncCallback(_ => Console.WriteLine(((JsonRpcStateAsync)_).Result));

            for (string line = Console.ReadLine(); !string.IsNullOrEmpty(line); line = Console.ReadLine())
            {
                var async = new JsonRpcStateAsync(rpcResultHandler, null);
                async.JsonRpc = line;
                JsonRpcProcessor.Process(async);
            }
        }
Clone this wiki locally