Пример #1
0
        private async Task <T> RequestToServer <T>(AbstractNamingRequest request)
            where T : CommonResponse
        {
            try
            {
                request.PutAllHeader(GetSecurityHeaders());
                request.PutAllHeader(GetSpasHeaders(NamingUtils.GetGroupedNameOptional(request.ServiceName, request.GroupName)));

                CommonResponse response =
                    requestTimeout < 0
                        ? await rpcClient.Request(request)
                        : await rpcClient.Request(request, requestTimeout);

                if (response == null)
                {
                    throw new NacosException(NacosException.SERVER_ERROR, "Request nacos server failed: RequestToServer<T>");
                }

                if (response.ResultCode != 200)
                {
                    throw new NacosException(response.ErrorCode, response.Message);
                }

                if (response is T)
                {
                    return((T)response);
                }

                _logger?.LogError("Server return unexpected response '{0}', expected response should be '{1}'", response.GetType().Name, typeof(T).Name);
            }
            catch (NacosException e)
            {
                throw new NacosException(e.ErrorCode, $"Request nacos server failed: {e.ErrorCode}, {e.Message}");
            }
            catch (Exception e)
            {
                throw new NacosException(NacosException.SERVER_ERROR, $"Request nacos server failed: {e.Message}");
            }

            throw new NacosException(NacosException.SERVER_ERROR, "Server return invalid response");
        }