Method Names

[JsonRpceMethod] is required to include your method as one exposed by your service. By default it uses the name of your method. You can override this by specifying your own in the parameter: eg [JsonRpcMethod("YourMethodName")]


There are multiple ways to control exceptions that are returned from your method.
  1. The default behavior is that any exception thrown in your method will be returned to the client in its entirety (stack trace, everything)
  2. There is a global hook you can use to manage exceptions before they are returned to the client. As early as possible you will want to call AustinHarris.JsonRpc.Config.SetErrorHandler(). In you should do this in Application_Start. The signature for this method is
private JsonRpcException OnJsonRpcException(JsonRequest rpc, JsonRpcException ex)
            return ex;
Here is where you would add your global error logging. Here you can clean up the exception, maybe you don't want to return the stack trace, or watson buckets.
  1. You can also explicitly throw the exception how you want to in the first place. If you throw a JsonRpcException from within your JsonRpcMethod then it will be returned to the client exactly as you specify it.
  2. Finally if you don't want to throw an exception at all but still want to control the error response to the client you can add an extra parameter to the end of your JsonRpcMethod. Ex:
        private string DoesNotThrowException(string s, ref JsonRpcException refException)
            refException = new JsonRpcException(-1, "refException worked", null);
            return s;
The JsonRpcException must always be the last parameter in your JSONRPCMethod or it will not work. It MUST be a 'Ref' parameter. If you don't want to return an exception to the client you must set it to null.


There are special cases were you may find that you need more information than is available in your input parameters. In it is common to pull a clients IP Addres out of the HttpContext.Current.Request object. There is a similar feature provided in Json-Rpc.Net.
From within one of your JsonRpcMethods you can make a call like:
        private string myIp()
            var req = JsonRpcContext.Current().Value as System.Web.HttpRequest;
            if (req != null)
                return req.UserHostAddress;
            return "IP not available";
The object returned from JsonRpcContext.Current().Value is a System.Web.HttpRequest only if you use the JsonRpcHandler from the JsonRpc.Net ASP.Net Handler package. And of course you would have to be hosting it in
If you are not hosting in ASP.Net then it is likely that you have your own JsonRpcProcessor.Process code. It is here where you are able to set the context as you desire, eg:
//JsonRpcProcessor.Process(JsonRpcStateAsync async, object context = null){...}
     var myAsync= new JsonRpcStateAsync(...);
     myAsync.JsonRpc = "{method:'yourMethod',params:[],id:0}";
     var myCusomContext =new {}; // ..Anything..; But be consistant. :)

Last edited Sep 19, 2012 at 5:33 AM by AustinHarris, version 4


No comments yet.