You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hüseyin Uslu edited this page Jun 3, 2014
·
1 revision
If you need to hook into the execution pipeline before your JsonRpcMethod is invoked, then you can do this by making a call to Config.SetPreProcessHandler as early as possible. If your using Asp.net then you could do this in Global.Application_Start.
protectedvoidApplication_Start(objectsender,EventArgse){Config.SetErrorHandler(OnJsonRpcException);// Set the Error HandlerConfig.SetPreProcessHandler(newPreProcessHandler(PreProcess));// Set the pre Processing Handler}
This is an example Preprocess handler. It has access to all of the Json-Rpc request information, and also has access to the JsonRpcContext.Current().Value. The context is passed in but also runs under the same context that the JsonRpcMethod does, so calling JsonRpcContext is still valid, though not needed as the context is passed as a parameter.
/// <summary>/// This method is invoked prior to ANY, and ALL JsonRpcMethods that have been registered./// </summary>/// <param name="rpc">The JSON-RPC request as it has been parsed</param>/// <param name="context">Because this is ASP.NET the context will be set as an instance of HttpRequest</param>/// <returns></returns>privateJsonRpcExceptionPreProcess(JsonRequestrpc,objectcontext){// Useful for logging or authentication using the context.if(!string.Equals(rpc.Method,"RequiresCredentials",StringComparison.CurrentCultureIgnoreCase))returnnull;// If this is using the ASP.Net handler then the context will contain the httpRequest// you could use that for cookies or IP authentication.varhttpRequest=contextasHttpRequest;// Here we will just check that the first parameter is a magic Key// DO NOT do this type of thing in production code. You would be better just checking the parameter inside the JsonRpcMethod. This is here just to show access to the JsonRequestvarj=rpc.ParamsasNewtonsoft.Json.Linq.JArray;if(j==null||j[0]==null||j[0].Type!=Newtonsoft.Json.Linq.JTokenType.String||!string.Equals(j[0].ToString(),"GoodPassword",StringComparison.CurrentCultureIgnoreCase))returnnewJsonRpcException(-2,"This exception was thrown using: JsonRpcTest.Global.PreProcess, Not Authenticated",null);returnnull;}