Skip to content
Hüseyin Uslu edited this page Jun 3, 2014 · 1 revision

There are special cases were you may find that you need more information than is available in your input parameters. In ASP.net 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:

        [JsonRpcMethod]
        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 ASP.net.

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. :)
     JsonRpcProcessor.Process(myAsync,myCustomContext);