Ejemplo n.º 1
0
        public static async Task <RemoteCallBackData> Invoke(string path, IDictionary <string, object> paras, string httpMethod, IContainer container)
        {
            IRemoteServiceExecutor remoteServiceInvoker = container.Resolve <IRemoteServiceExecutor>();
            ISerializer            converter            = container.Resolve <ISerializer>();
            RemoteCallBackData     result = await remoteServiceInvoker.InvokeAsync(path, paras, httpMethod);

            if (!string.IsNullOrEmpty(result.ExceptionMessage))
            {
                throw new HttpStatusCodeException(400, $"{result.ToErrorString()}", path);
            }

            if (!string.IsNullOrEmpty(result.ErrorCode) || !string.IsNullOrEmpty(result.ErrorMsg))
            {
                if (int.TryParse(result.ErrorCode, out int erroCode) && erroCode > 200 && erroCode < 600)
                {
                    throw new HttpStatusCodeException(erroCode, result.ToErrorString(), path);
                }

                return(new RemoteCallBackData {
                    ErrorCode = result.ErrorCode, ErrorMsg = result.ErrorMsg
                });
            }
            if (result.ResultType == typeof(FileData).ToString())
            {
                object file = converter.Deserialize(result.Result, typeof(FileData));
                result.Result = file;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <RemoteCallBackData> InvokeAsync(string serviceIdOrPath, IDictionary <string, object> paras, string httpMethod = "get", string token = null)
        {
            if (paras == null)
            {
                paras = new ConcurrentDictionary <string, object>();
            }

            if (!(_cache != null && _cache.TryGet(serviceIdOrPath + "_" + httpMethod.ToLower(), out List <ServerAddress> service)))
            {
                service = await GetServiceByPathAsync(serviceIdOrPath, httpMethod);
            }


            if (service == null || service.Count == 0)
            {
                return(new RemoteCallBackData
                {
                    ErrorCode = "404",
                    ErrorMsg = $"路径为:{serviceIdOrPath}, 没有找到!"
                });
            }

            if (token == null && _serviceTokenGetter?.GetToken != null)
            {
                token = _serviceTokenGetter.GetToken();
            }

            RemoteCallBackData result = await InvokeAsync(service, serviceIdOrPath, paras, token);

            if (!string.IsNullOrEmpty(result.ExceptionMessage))
            {
                return(new RemoteCallBackData
                {
                    ErrorCode = "400",
                    ErrorMsg = $"{serviceIdOrPath}, {result.ToErrorString()}"
                });
            }

            if (string.IsNullOrEmpty(result.ErrorCode) && string.IsNullOrEmpty(result.ErrorMsg))
            {
                return(result);
            }

            if (int.TryParse(result.ErrorCode, out int erroCode) && erroCode > 200 && erroCode < 600)
            {
                return(new RemoteCallBackData
                {
                    ErrorCode = result.ErrorCode,
                    ErrorMsg = $"{serviceIdOrPath}, {result.ToErrorString()}"
                });
            }

            return(result);
        }