Created
February 3, 2009 16:49
-
-
Save atifaziz/57613 to your computer and use it in GitHub Desktop.
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; | |
using System.Collections; | |
using System.Diagnostics; | |
using Jayrock.JsonRpc; | |
using Jayrock.Services; | |
public class MyJsonRpcDispatcher : JsonRpcDispatcher | |
{ | |
public MyJsonRpcDispatcher(IService service) : | |
base(service) { } | |
protected override object OnError(Exception e, IDictionary request) | |
{ | |
// Give the service a chance to log exceptions | |
// See issue #2: http://code.google.com/p/jayrock/issues/detail?id=2 | |
var logging = Service as IExceptionLogging; | |
if (logging != null) | |
logging.Log(e); | |
var error = base.OnError(e, request) as IDictionary; | |
// Customize the error object before returning | |
// See issue #6: http://code.google.com/p/jayrock/issues/detail?id=6 | |
if (error != null) | |
error["source"] = e.Source; | |
return error; | |
} | |
} | |
public interface IExceptionLogging | |
{ | |
void Log(Exception e); | |
} | |
[JsonRpcService] | |
public class DemoService : JsonRpcService, IExceptionLogging | |
{ | |
[JsonRpcMethod] | |
public void Foo() | |
{ | |
throw new ApplicationException(); | |
} | |
void IExceptionLogging.Log(Exception e) | |
{ | |
Trace.TraceError(e.Message); // Trace all exceptions | |
} | |
} | |
public static class Program | |
{ | |
public static void Main() | |
{ | |
Trace.Listeners.Add(new TextWriterTraceListener(Console.Error)); | |
// Register own dispatcher implementation. | |
JsonRpcDispatcherFactory.Current = service => new MyJsonRpcDispatcher(service); | |
var demo = new DemoService(); | |
var dispatcher = JsonRpcDispatcherFactory.CreateDispatcher(demo); | |
// Call a method that throws an exception... | |
Console.WriteLine(dispatcher.Process("{ id: 1, method: Foo }")); | |
Console.WriteLine(); | |
// Call a method that does not exist... | |
Console.WriteLine(dispatcher.Process("{ id: 2, method: Bar }")); | |
Console.WriteLine(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment