Exemplo n.º 1
0
        /// <summary>
        /// 对服务请求进行处理,根据路由信息调用合适的服务实现
        /// </summary>
        /// <param name="context">服务上下文,每次服务调用共享一个实例</param>
        /// <returns></returns>
        public override async Task <ServiceProcessResult> ProcessAsync(ServiceContext context)
        {
            logger.LogInformation($"Service request process beginning. ServiceId={context.Route.ServiceId}, ActionId={context.Route.ActionId}");

            if (!context.Route.Enabled)
            {
                logger.LogError($"Route is disabled. ServiceId={context.Route.ServiceId}, ActionId={context.Route.ActionId}");
                return(ServiceProcessorUtils.CreateServiceExceptionResult(ServiceExceptionKeys.SERVICE_DISABLED_ERROR, ProtocolStackFactory));
            }

            if (Serializer == null || ServiceInvoker == null)
            {
                logger.LogError($"Service has configuration error. ServiceId={context.Route.ServiceId}, ActionId={context.Route.ActionId}");
                return(ServiceProcessorUtils.CreateServiceExceptionResult(ServiceExceptionKeys.SERVER_CONFIG_ERROR, ProtocolStackFactory));
            }

            var result = new ServiceProcessResult()
            {
                ServiceResponse = ProtocolStackFactory.CreateServiceResponse()
            };

            try
            {
                result.ServiceResponse.ReturnValue = await ServiceInvoker.Invoke(Serializer, context.Route, context.ActionParamList);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"ServiceInvoker.Invoke has error, ServiceId={context.Route.ServiceId}, ActionId={context.Route.ActionId}, ExceptionMessage={ex.Message}");
                return(ServiceProcessorUtils.CreateServiceExceptionResult(ServiceExceptionKeys.SERVICE_INVOKE_ERROR, ProtocolStackFactory));
            }

            logger.LogInformation($"Service request process finished. ServiceId={context.Route.ServiceId}, ActionId={context.Route.ActionId}");

            return(result);
        }
Exemplo n.º 2
0
        private async Task <IServiceRequest> CreateServiceRequestAsync(int serviceId, int actionId, object[] paramList)
        {
            var request = ProtocolStackFactory.CreateServiceRequest();

            request.ServiceId = serviceId;
            request.ActionId  = actionId;
            request.ParamList = new List <byte[]>();

            if (paramList != null)
            {
                foreach (var param in paramList)
                {
                    try
                    {
                        request.ParamList.Add(await Serializer.SerializeAsync(param));
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(ex, $"NodeClient serialize parameters has error. Host={Host}, Port={Port}, ServiceId={serviceId}, ActionId={actionId}, ExceptionMessage={ex.Message}");
                        throw ex;
                    }
                }
            }

            return(request);
        }
Exemplo n.º 3
0
 private async Task <ServiceProcessResult> CallNext(ServiceContext context)
 {
     if (Next != null)
     {
         return(await Next.ProcessAsync(context));
     }
     return(new ServiceProcessResult()
     {
         ServiceResponse = ProtocolStackFactory.CreateServiceResponse()
     });
 }
Exemplo n.º 4
0
        /// <summary>
        /// 服务授权验证
        /// </summary>
        /// <param name="context">服务上下文,每次服务调用共享一个实例</param>
        /// <returns></returns>
        public override async Task <ServiceProcessResult> ProcessAsync(ServiceContext context)
        {
            try
            {
                await serviceAuthorizer.Validate(context, context.Route.ServiceId, context.Route.ActionId, context.Attachments);
            }
            catch (ServiceAuthorizeException ex)
            {
                logger.LogError(ex, $"Service authorize failed. ServiceId={context.Route.ServiceId}, ActionId={context.Route.ActionId}, Identity={context.Identity}, Remote={context.RemoteAddress.Address.ToIPString()}, Message={ex.Message}");
                switch (ex.ServiceAuthorizeExceptionType)
                {
                case ServiceAuthorizeExceptionType.NoAuthorize:
                    return(ServiceProcessorUtils.CreateServiceExceptionResult(ServiceExceptionKeys.SERVICE_NO_AUTHORIZE, ProtocolStackFactory));

                case ServiceAuthorizeExceptionType.DateLimit:
                    return(ServiceProcessorUtils.CreateServiceExceptionResult(ServiceExceptionKeys.SERVICE_DATE_LIMIT, ProtocolStackFactory));

                case ServiceAuthorizeExceptionType.TimeLimit:
                    return(ServiceProcessorUtils.CreateServiceExceptionResult(ServiceExceptionKeys.SERVICE_TIME_LIMIT, ProtocolStackFactory));

                default:
                    return(ServiceProcessorUtils.CreateServiceExceptionResult(ServiceExceptionKeys.SERVICE_NO_AUTHORIZE, ProtocolStackFactory));
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"Service authorize has error. ServiceId={context.Route.ServiceId}, ActionId={context.Route.ActionId}, Identity={context.Identity}, Remote={context.RemoteAddress.Address.ToIPString()}, ExceptionMessage={ex.Message}");
                return(ServiceProcessorUtils.CreateSystemExceptionResult(SystemExceptionKeys.SYSTEM_ERROR, ProtocolStackFactory));
            }

            if (Next != null)
            {
                return(await Next.ProcessAsync(context));
            }
            return(new ServiceProcessResult()
            {
                ServiceResponse = ProtocolStackFactory.CreateServiceResponse()
            });
        }
Exemplo n.º 5
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);
            }
        }