Exemplo n.º 1
0
        /// <summary>
        /// 调用远程服务
        /// </summary>
        /// <param name="actionType">Action类型</param>
        /// <param name="paramList">参数列表</param>
        /// <returns></returns>
        public virtual async Task <object> CallRemoteServiceAsync(MethodInfo actionType, object[] paramList)
        {
            ServiceProxyInfo serviceProxyInfo = GetServiceProxyInfo(actionType);

            if (serviceProxyInfo == null)
            {
                var actionTypeName = actionType != null ? actionType.Name : string.Empty;
                throw new InvalidOperationException($"Action not found. ActionTypeName={actionTypeName}");
            }

            logger.LogInformation($"Call remote service beginning. ProxyName={serviceProxyInfo.ProxyName}, ServiceId={serviceProxyInfo.ServiceId}, ActionId={serviceProxyInfo.ActionId}");

            logger.LogDebug($"Service proxy detail info. ProxyName={serviceProxyInfo.ProxyName}, ServiceId={serviceProxyInfo.ServiceId}, ActionId={serviceProxyInfo.ActionId}, Enabled={serviceProxyInfo.Enabled}, Timeout={serviceProxyInfo.Timeout}, ActionProxyType={serviceProxyInfo.ActionProxyType}, ReturnType={serviceProxyInfo.ReturnType}");

            if (!serviceProxyInfo.Enabled)
            {
                throw new InvalidOperationException($"Action is disabled. ProxyName={serviceProxyInfo.ProxyName}, ServiceId={serviceProxyInfo.ServiceId},ActionId={serviceProxyInfo.ActionId}");
            }

            logger.LogDebug($"Invoke ServiceCaller.CallAsync. ProxyName={serviceProxyInfo.ProxyName}, ServiceId={serviceProxyInfo.ServiceId}, ActionId={serviceProxyInfo.ActionId}, ParamList={(paramList == null || paramList.Length == 0 ? string.Empty : string.Join("|", paramList))}");
            ServiceCallResult result = await serviceCaller.CallAsync(nodeClientContainer, new ServiceCallInfo()
            {
                ProxyName   = serviceProxyInfo.ProxyName,
                ServiceId   = serviceProxyInfo.ServiceId,
                ActionId    = serviceProxyInfo.ActionId,
                ServiceName = serviceProxyInfo.ServiceName,
                ActionName  = serviceProxyInfo.ActionName,
                ParamList   = paramList,
                ReturnType  = serviceProxyInfo.ReturnType,
                Timeout     = serviceProxyInfo.Timeout
            });

            logger.LogInformation($"Call remote service finished. ProxyName={serviceProxyInfo.ProxyName}, ServiceId={serviceProxyInfo.ServiceId}, ActionId={serviceProxyInfo.ActionId}");

            return(result.ReturnVal);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 调用服务
        /// </summary>
        /// <param name="serviceId">服务Id</param>
        /// <param name="actionId">ActionId</param>
        /// <param name="paramList">Action参数列表</param>
        /// <param name="returnType">Action返回类型</param>
        /// <param name="timeout">Action调用超时时长(毫秒)</param>
        /// <param name="attachments">Action调用的附加数据</param>
        /// <returns></returns>
        public async Task <ServiceCallResult> CallServiceAsync(int serviceId, int actionId, object[] paramList, Type returnType, int timeout, IDictionary <string, byte[]> attachments)
        {
            logger.LogDebug($"Call service beginning. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}");

            logger.LogDebug($"Service detail info. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}, ParamList={(paramList == null || paramList.Length == 0 ? string.Empty : string.Join("|", paramList))}");

            var request = await CreateServiceRequestAsync(serviceId, actionId, paramList);

            RequestResult result = null;

            byte[] requestBytes = null;

            try
            {
                requestBytes = await Serializer.SerializeAsync(request);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"NodeClient serialize request data has error. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}, ExceptionMessage={ex.Message}");
                throw ex;
            }

            try
            {
                logger.LogDebug($"Send request data. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}");
                result = await client.SendAsync(requestBytes, timeout, attachments);
            }
            catch (RequestTimeoutExcption ex)
            {
                logger.LogError(ex, $"NodeClient call service timeout. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}, RequestId={ex.Request.Id}");
                throw ex;
            }
            catch (NetworkException ex)
            {
                logger.LogError(ex, $"NodeClient call service has network error. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}, ExceptionMessage={ex.Message}");
                throw ex;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"NodeClient call service has error. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}, ExceptionMessage={ex.Message}");
                throw ex;
            }

            IServiceResponse response = null;

            try
            {
                logger.LogDebug($"NodeClient deserialize response data. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}");
                response = (IServiceResponse)await Serializer.DeserializeAsync(ProtocolStackFactory.ServiceResponseType, result.Data);

                if (response == null)
                {
                    response = ProtocolStackFactory.CreateServiceResponse();
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"NodeClient deserialize response data has error: Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}, ExceptionMessage={ex.Message}");
                throw ex;
            }

            if (response.HasException)
            {
                logger.LogError($"Node server has an error, Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}, ExceptionId={response.ExceptionId}, ExceptionMessage={response.ExceptionMessage}");
                throw new ServiceCallException(serviceId, actionId, response.ExceptionId, response.ExceptionMessage);
            }
            else
            {
                var serviceCallResult = new ServiceCallResult()
                {
                    Attachments = result.Attachments
                };
                if (returnType != null && returnType != typeof(void))
                {
                    try
                    {
                        logger.LogDebug($"NodeClient deserialize return value. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}");
                        serviceCallResult.ReturnVal = await Serializer.DeserializeAsync(returnType, response.ReturnValue);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"NodeClient deserialize return value has error. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}, ExceptionMessage={ex.Message}");
                        throw ex;
                    }
                }
                logger.LogDebug($"Call service finished. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}");
                return(serviceCallResult);
            }
        }