Esempio n. 1
0
        /// <summary>异步调用,等待返回结果</summary>
        /// <param name="resultType">返回类型</param>
        /// <param name="action">服务操作</param>
        /// <param name="args">参数</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public virtual async Task <Object> InvokeAsync(Type resultType, String action, Object args = null, Byte flag = 0)
        {
            var ss = Client;

            if (ss == null)
            {
                return(null);
            }

            var act = action;

            try
            {
                return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
            }
            catch (ApiException ex)
            {
                // 重新登录后再次调用
                if (ex.Code == 401)
                {
                    return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
                }

                throw;
            }
            // 截断任务取消异常,避免过长
            catch (TaskCanceledException ex)
            {
                throw new TaskCanceledException(action + "超时取消", ex);
            }
        }
Esempio n. 2
0
        /// <summary>异步调用,等待返回结果</summary>
        /// <param name="resultType">返回类型</param>
        /// <param name="action">服务操作</param>
        /// <param name="args">参数</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public virtual async Task <Object> InvokeAsync(Type resultType, String action, Object args = null, Byte flag = 0)
        {
            // 让上层异步到这直接返回,后续代码在另一个线程执行
#if !NET4
            await Task.Yield();
#endif

            Open();

            var act = action;

            try
            {
                return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
            }
            catch (ApiException ex)
            {
                // 重新登录后再次调用
                if (ex.Code == 401)
                {
                    await Cluster.InvokeAsync(client => OnLoginAsync(client, true));

                    return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
                }

                throw;
            }
            // 截断任务取消异常,避免过长
            catch (TaskCanceledException)
            {
                throw new TaskCanceledException($"[{act}]超时[{Timeout:n0}ms]取消");
            }
        }
Esempio n. 3
0
        /// <summary>异步调用,等待返回结果</summary>
        /// <param name="resultType">返回类型</param>
        /// <param name="action">服务操作</param>
        /// <param name="args">参数</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        public virtual async Task <Object> InvokeAsync(Type resultType, String action, Object args = null, Byte flag = 0)
        {
#if !NET4
            // 让上层异步到这直接返回,后续代码在另一个线程执行
            await Task.Yield();
#endif

            Open();

            var act = action;

            try
            {
                return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
            }
            catch (ApiException ex)
            {
                // 重新登录后再次调用
                if (ex.Code == 401)
                {
                    return(await ApiHostHelper.InvokeAsync(this, this, resultType, act, args, flag));
                }

                throw;
            }
            // 截断任务取消异常,避免过长
            catch (TaskCanceledException ex)
            {
                throw new TaskCanceledException($"[{action}]超时取消", ex);
            }
        }
Esempio n. 4
0
        /// <summary>调用</summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="action"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public async Task <TResult> InvokeAsync <TResult>(String action, object args = null)
        {
            var ss = Client;

            if (ss == null)
            {
                return(default(TResult));
            }

            return(await ApiHostHelper.InvokeAsync <TResult>(this, this, action, args).ConfigureAwait(false));
        }
Esempio n. 5
0
        /// <summary>调用</summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="action"></param>
        /// <param name="args"></param>
        /// <param name="cookie">附加参数,位于顶级</param>
        /// <returns></returns>
        public virtual async Task <TResult> InvokeAsync <TResult>(String action, Object args = null, IDictionary <String, Object> cookie = null)
        {
            var ss = Client;

            if (ss == null)
            {
                return(default(TResult));
            }

            // 未登录且设置了用户名,并且当前不是登录,则异步登录
            if (!Logined && !UserName.IsNullOrEmpty() && action != "Login")
            {
                await LoginAsync();
            }

            var act = action;

            if (!ActionPrefix.IsNullOrEmpty() && !act.Contains("/"))
            {
                act = ActionPrefix.EnsureEnd("/") + act;
            }

            LastInvoke = DateTime.Now;
            try
            {
                return(await ApiHostHelper.InvokeAsync <TResult>(this, this, act, args, cookie ?? Cookie));
            }
            catch (ApiException ex)
            {
                // 重新登录后再次调用
                if (ex.Code == 401)
                {
                    Logined = false;
                    // 如果当前不是登录,且设置了用户名,尝试自动登录
                    if (action != "Login" && !UserName.IsNullOrEmpty())
                    {
                        await LoginAsync();

                        return(await ApiHostHelper.InvokeAsync <TResult>(this, this, act, args, cookie ?? Cookie));
                    }
                }

                throw;
            }
            // 截断任务取消异常,避免过长
            catch (TaskCanceledException)
            {
                throw new TaskCanceledException(action + "超时取消");
            }
        }
Esempio n. 6
0
        /// <summary>调用</summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="action"></param>
        /// <param name="args"></param>
        /// <param name="cookie">附加参数,位于顶级</param>
        /// <returns></returns>
        public virtual async Task<TResult> InvokeAsync<TResult>(String action, Object args = null, IDictionary<String, Object> cookie = null)
        {
            var ss = Client;
            if (ss == null) return default(TResult);

            //if (!Active || !ss) Logined = false;

            // 未登录且设置了用户名,并且当前不是登录,则异步登录
            if (!Logined && !UserName.IsNullOrEmpty() && action != LoginAction) await LoginAsync();

            LastInvoke = DateTime.Now;
            try
            {
                return await ApiHostHelper.InvokeAsync<TResult>(this, this, action, args, cookie ?? Cookie).ConfigureAwait(false);
            }
            catch (ApiException ex)
            {
                // 重新登录后再次调用
                if (ex.Code == 401)
                {
                    Logined = false;
                    // 如果当前不是登录,且设置了用户名,尝试自动登录
                    if (action != LoginAction && !UserName.IsNullOrEmpty())
                    {
                        await LoginAsync();
                        return await ApiHostHelper.InvokeAsync<TResult>(this, this, action, args, cookie ?? Cookie).ConfigureAwait(false);
                    }
                }

                throw;
            }
            // 截断任务取消异常,避免过长
            catch (TaskCanceledException)
            {
                throw new TaskCanceledException(action + "超时取消");
            }
        }
Esempio n. 7
0
 /// <summary>远程调用</summary>
 /// <typeparam name="TResult"></typeparam>
 /// <param name="action"></param>
 /// <param name="args"></param>
 /// <param name="cookie">附加参数,位于顶级</param>
 /// <returns></returns>
 public async Task <TResult> InvokeAsync <TResult>(String action, Object args = null, IDictionary <String, Object> cookie = null)
 {
     return(await ApiHostHelper.InvokeAsync <TResult>(_Host, this, action, args, cookie).ConfigureAwait(false));
 }
Esempio n. 8
0
 /// <summary>远程调用</summary>
 /// <typeparam name="TResult"></typeparam>
 /// <param name="action">服务操作</param>
 /// <param name="args">参数</param>
 /// <param name="flag">标识</param>
 /// <returns></returns>
 public async Task <TResult> InvokeAsync <TResult>(String action, Object args = null, Byte flag = 0) => (TResult)await ApiHostHelper.InvokeAsync(_Host, this, typeof(TResult), action, args, flag);
Esempio n. 9
0
        /// <summary>指定客户端的异步调用,等待返回结果</summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="client">客户端</param>
        /// <param name="action">服务操作</param>
        /// <param name="args">参数</param>
        /// <param name="flag">标识</param>
        /// <returns></returns>
        protected virtual async Task <TResult> InvokeWithClientAsync <TResult>(ISocketClient client, String action, Object args = null, Byte flag = 0)
        {
            var act = action;

            return((TResult)await ApiHostHelper.InvokeAsync(this, client, typeof(TResult), act, args, flag));
        }
Esempio n. 10
0
 /// <summary>远程调用</summary>
 /// <typeparam name="TResult"></typeparam>
 /// <param name="action"></param>
 /// <param name="args"></param>
 /// <returns></returns>
 public async Task <TResult> InvokeAsync <TResult>(string action, object args = null)
 {
     return(await ApiHostHelper.InvokeAsync <TResult>(_ApiHost, this, action, args).ConfigureAwait(false));
 }