Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created July 18, 2013 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifaziz/6027119 to your computer and use it in GitHub Desktop.
Save atifaziz/6027119 to your computer and use it in GitHub Desktop.
Custom Jayrock JsonRpcDispatcher demonstrating how to get the JSON-RPC request Id in the called method; https://groups.google.com/forum/#!topic/jayrock/J8MgyMgwrPI
using System.Collections;
using System.Web;
using Jayrock.Json;
using Jayrock.JsonRpc;
using Jayrock.Services;
public sealed class CustomJsonRpcDispatcher : JsonRpcDispatcher
{
const string RequestIdKey = "JsonRpcRequestId";
public CustomJsonRpcDispatcher(IService service) :
base(service) {}
public static object GetRequestIdFromCurrentHttpContext()
{
return GetRequestId(null);
}
public static object GetRequestId(HttpContext context)
{
return (context ?? HttpContext.Current).Items["JsonRpcRequestId"];
}
protected override IDictionary ParseRequest(JsonReader input)
{
var request = base.ParseRequest(input);
HttpContext.Current.Items[RequestIdKey] = request["id"];
return request;
}
}
<%@ Application Language="C#" %>
<%@ Import Namespace="Jayrock.JsonRpc" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
JsonRpcDispatcherFactory.Current = service => new CustomJsonRpcDispatcher(service);
}
</script>
<%@ WebHandler Class="HelloWorld" Language="C#" %>
using System.Web;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;
public class HelloWorld : JsonRpcHandler
{
[JsonRpcMethod("echoTest")]
public string EchoTest(string test)
{
var requestId = CustomJsonRpcDispatcher.GetRequestId(HttpContext.Current);
return "Response " + requestId + " : " + test;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment