Created
July 18, 2013 06:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Application Language="C#" %> | |
<%@ Import Namespace="Jayrock.JsonRpc" %> | |
<script runat="server"> | |
void Application_Start(object sender, EventArgs e) | |
{ | |
JsonRpcDispatcherFactory.Current = service => new CustomJsonRpcDispatcher(service); | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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