Option to retrieve IP address

May 9, 2012 at 10:59 PM

I am sure I am not the only one with a need to collect the user host IP address of

Json-rpc.NET request. What I suggest is to implement some kind of access to request HttpContext data. There is some doctrine issue as json-rpc is not intertwined with http. Http is just a transport layer, but json-rpc is independent and above it. But json-rpc.NET practically intertwines this two together so I guess there would be no religious harm done if this will be done.

My suggestion and this is just a suggestion as better solution might exist. Let's expose member context (of type HttpContext) in JsonRpcService. If I am not mistaken the instance of class is recreated for every request served so this might even be static.

May 10, 2012 at 12:06 AM
Edited May 10, 2012 at 12:08 AM

I agree that this is a good problem to solve, and would like to solve it. I have not yet thought of a clean way to solve it.

Some of my thinking is:

  • JsonRpcService derived services are only a single instance, they are not enforced to be static classes, but from the point of view of the RpcHandler they might as well be static classes. This means that I cannot insert some context data into the service when a method is invoked, without killing concurrency by locking around each method call into the service class to ensure the state available during the scope of the method call is the correct context.
  • Creating a new instance of the service for each request would affect performance, and could really hurt it depending on service class.  
  • Could maybe add another type of JsonRpcService base class that assumes it will be created on each request, and would be typed specifically to receive an HTTPContext. This would mean that the method signatures for your JsonRpcMethod's would be unaffected.
  • Could add another property to the [JsonRpcMethod] attribute that lets you specify an input parameter that is an HttpContext. This would allow the parameter to be hidden from jsonRpc, but be injected by the RpcHandler. This should also minimize performance impact as it would not require creating new instances of a JsonRpcService object for each request.
  • I would like to add/maintain support for using JsonRpc.Net over any protocol. I know it it not documented, but you can invoke a JsonRpcMethod from anywhere by calling AustinHarris.JsonRpc.Handler.Current.Handle(JsonRequest rpc) which is what the asp.net handler does internally when it calls into JsonRpcProcessor. So currently the core method dispatching engine knows nothing about the protocol that is using it.

I will continue thinking about other solutions and the trade-offs of the ones I have posted. Additional ideas are quite welcome.

May 14, 2012 at 11:24 AM

I agree with idea that JsonRpc.Net should be kept independent from protocol. From what you have written the most promising option seems to me the introduction of an attribute that would specify which input parameter is HttpContext. If request originates from other transport it could be simply set to null. I think this would not break existing behavior.

May 15, 2012 at 12:49 AM

I'm going to start down this route as soon as I have the exceptions as parameters working.

Jun 25, 2012 at 2:00 PM

Any news yet?

Jun 25, 2012 at 4:29 PM

I finished the work I wanted to do on the exceptions. So this is next on my list. I'll see if I can get into it today and make some progress.

Jul 25, 2012 at 7:57 PM

Any news yet?

Aug 10, 2012 at 3:58 AM
and3rson wrote:

I am sure I am not the only one with a need to collect the user host IP address...

You are not - this project looked promising but with having any HttpContext available it fails my needs

Nonetheless, good work!

Aug 10, 2012 at 4:00 AM

Hi Matthew,

I have made a change to this code to include the context and a few other small changes. I haven't had the time to push the change set to AustinHarris.

Ill try and get onto it asap


Aug 10, 2012 at 4:07 AM

I have to apologize gentlemen.

I have a new job, and am up against some deadlines. I have not been able to put any time into this recently.

I would love to have something implimented similar to HttpContext.Current, Or OperationContext.Current. But I havn't looked into how those are implemented.

I'll look forward to seeing your approach to solve this issue.



Sep 16, 2012 at 5:14 PM

I guess it is safe to assume this project is dead.

Sep 18, 2012 at 3:56 AM
Edited Sep 18, 2012 at 3:57 AM
and3rson wrote:

I guess it is safe to assume this project is dead.

I just pushed in a change set that will let you do the following. 

        private string myIp()
            var req = JsonRpcContext.Current().Value as System.Web.HttpRequest;
            if (req != null)
                return req.UserHostAddress;
            return "hmm. no ip";

        private string myUserAgent()
            var req = JsonRpcContext.Current().Value as System.Web.HttpRequest;
            if (req != null)
                return req.UserAgent.ToString();
            return "hmm. no UserAgent";

I just threw it together 30 minutes ago, so you may want to test it a little more then I did.

If you do write some tests, I would be happy to include them in the project.


Let me know if this works for you and I'll create a new build.