public void Invoke(string method, object[] args, Action <object, Exception> resultCallback) { if (string.IsNullOrEmpty(method)) { throw new ArgumentNullException("method"); } var jreq = new JsonRpcRequest(method, args); var syncCtx = SynchronizationContext.Current; jreq.Post(this.Uri, (jrep, postError) => { syncCtx.Post(delegate { if (postError != null) { resultCallback(null, postError); return; } else { if (jrep.Error == null) { resultCallback(jrep.Result, null); } else { var msg = String.Format("Failed to invoke JSON-RPC: {0}", jrep.Error); var error = new JsonRpcException(msg, jrep.Error); resultCallback(null, error); } } }, null); }); }
public Task <object> InvokeTaskAsync(string method, object[] args) { var jreq = new JsonRpcRequest(method, args); var mainTask = jreq.PostTaskAsync(this.Uri).ContinueWith <object>(task => { var error = task.Result.Error; if (error != null) { var ex = new JsonRpcException("调用JSON-RPC 失败", error); throw ex; } else { return(task.Result); } }); return(mainTask); }