/// <summary>调用</summary> /// <typeparam name="TResult"></typeparam> /// <param name="host"></param> /// <param name="session"></param> /// <param name="action"></param> /// <param name="args"></param> /// <param name="cookie">附加参数,位于顶级</param> /// <returns></returns> public static async Task <TResult> InvokeAsync <TResult>(IApiHost host, IApiSession session, String action, Object args, IDictionary <String, Object> cookie) { if (session == null) { return(default(TResult)); } var enc = host.Encoder; //var data = enc.Encode(action, args, cookie); var data = enc.Encode(new { action, args }.ToDictionary().Merge(cookie)); var msg = session.CreateMessage(data); // 过滤器 host.ExecuteFilter(session, msg, true); var rs = await session.SendAsync(msg).ConfigureAwait(false); if (rs == null) { return(default(TResult)); } // 过滤器 host.ExecuteFilter(session, rs, false); // 特殊返回类型 if (typeof(TResult) == typeof(Packet)) { return((TResult)(Object)rs.Payload); } var dic = enc.Decode(rs.Payload); if (typeof(TResult) == typeof(IDictionary <String, Object>)) { return((TResult)(Object)dic); } //return enc.Decode<TResult>(dic); var code = 0; Object result = null; //enc.TryGet(dic, out code, out result); Object cod = null; dic.TryGetValue("code", out cod); // 参数可能不存在 dic.TryGetValue("result", out result); code = cod.ToInt(); // 是否成功 if (code != 0) { throw new ApiException(code, result + ""); } if (result == null) { return(default(TResult)); } if (typeof(TResult) == typeof(Object)) { return((TResult)result); } // 返回 return(enc.Convert <TResult>(result)); }