Beispiel #1
0
        /// <summary>
        /// 服务执行
        /// </summary>
        /// <param name="metadata"></param>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private object doExcuteService(ServiceRegistData service, ServiceRequest request, Action <object> responseAction, RpcCallContext context)
        {
            var method = new RpcMethodExcute();
            var result = method.Excute(service, request, responseAction, context);

            return(result);
        }
Beispiel #2
0
        private ServiceResponse getResponseServiceData(ServiceRegistData service, ServiceResponse response)
        {
            if (response.ServiceData == null)
            {
                response.ServiceData = new ResponseServiceData();
            }
            if (response.ServiceMetadata == null)
            {
                response.ServiceMetadata = new ResponseServiceMetadata();
            }
            if (service != null)
            {
                response.ServiceMetadata.ServiceId        = service.RegistData.ServiceId;
                response.ServiceMetadata.ServiceProvderId = service.RegistData.ServiceProvderId;
                response.ServiceMetadata.ServiceName      = service.RegistData.ServiceName;
                response.ServiceMetadata.ServiceInfc      = service.RegistData.ServiceInfc;
                response.ServiceMetadata.ServiceImpl      = service.RegistData.ServiceImpl;
                response.ServiceMetadata.ServiceMethod    = service.RegistData.ServiceMethod;
                response.ServiceMetadata.ServiceHandler   = service.RegistData.ServiceHandler;
                response.ServiceMetadata.Model            = service.RegistData.Model;
                response.ServiceMetadata.Auth             = service.RegistData.Auth;
            }
            response.ServiceData.ServiceCode     = 0;
            response.ServiceData.ServiceMsg      = string.Empty;
            response.ServiceData.ServiceError    = string.Empty;
            response.ServiceData.ServiceData     = string.Empty;
            response.ServiceData.ServiceDataType = string.Empty;

            return(response);
        }
Beispiel #3
0
        /// <summary>
        /// 服务执行
        /// </summary>
        /// <param name="metadata"></param>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        private object doExcute(ServiceRegistData service, ServiceRequest request, Action <object> responseAction, RpcCallContext context)
        {
            var typeParams = new List <object>();

            typeParams.Add(service);
            typeParams.Add(request);
            typeParams.Add(context);
            if (context?.ServerCallContext != null)
            {
                typeParams.Add(context.ServerCallContext);
            }


            var paramValue = context?.PeerAddress?.IpAddress;

            var nameParams = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            if (!string.IsNullOrWhiteSpace(paramValue))
            {
                nameParams.Add("ip", paramValue);
            }
            else
            {
                nameParams.Add("ip", "127.0.0.1");
            }
            var result = XRunner.Invoke(service.ServiceImpl, request.ServiceData.ServiceMethod, request.ServiceData.ServiceParam, nameParams, typeParams, responseAction);

            return(result);
        }
Beispiel #4
0
 private ServiceResponse getServiceResponse(ServiceRegistData service, ServiceResponse response)
 {
     if (response == null)
     {
         response = new ServiceResponse();
     }
     response = getResponseServiceData(service, response);
     response = getResponseTraceData(service, response);
     return(response);
 }
Beispiel #5
0
 public ServiceRequest GetServiceRequest(ServiceRegistData service, ServiceRequest request)
 {
     if (request == null)
     {
         request = new ServiceRequest();
     }
     request = getRequestServiceData(service, request);
     request = getRequestTraceData(service, request);
     return(request);
 }
Beispiel #6
0
        private void registerService(RegistData data)
        {
            if (data == null || string.IsNullOrWhiteSpace(data.ServiceInfc) ||
                string.IsNullOrWhiteSpace(data.ServiceImpl))
            {
                return;
            }
            var value = new ServiceRegistData(data);

            servicestore.TryAdd(Guid.NewGuid().ToString(), value);
        }
Beispiel #7
0
 public ServiceResponse GetResponse(ServiceRegistData service, ServiceRequest request, object data, Exception ex)
 {
     if (ex == null)
     {
         return(successServiceResponse(service, request, data));
     }
     else
     {
         return(errServiceResponse(service, request, ex));
     }
 }
        public ServiceHandlerHelper(ServiceRegistData data)
        {
            this.serviceRegistData = data;
            var type = data.ServiceHandler;

            if (type == null)
            {
                return;
            }
            this.rpcServiceHandler = Activator.CreateInstance(type, null) as RpcServiceHandler;
        }
Beispiel #9
0
        private void responseAction(ServiceRegistData service, ServiceRequest request, object data, IServerStreamWriter <RpcResponse> responseStream)
        {
            if (responseStream == null)
            {
                return;
            }
            var d        = ResponseHelper.Instance.GetResponse(service, request, data, null);
            var response = new RpcResponse();

            response.Data = d.ToJson();
            responseStream.WriteAsync(response).Wait();
        }
Beispiel #10
0
        private ServiceResponse successServiceResponse(ServiceRegistData service, ServiceRequest request, object data)
        {
            sendMsg($"RPC Success>>[{request.TraceData.RequestId}.{request.TraceData.TraceId}]>>[{request.ServiceData.ServiceInfc}].[{request.ServiceData.ServiceMethod}]");
            var result = getServiceResponse(service, null);

            if (data != null)
            {
                result.ServiceData.ServiceData     = data.ToJson();
                result.ServiceData.ServiceDataType = data.GetType().AssemblyQualifiedName;
            }
            return(result);
        }
Beispiel #11
0
 private ServiceResponse doRsfService(ServiceRegistData service, ServiceRequest request, Action <object> responseAction, RpcCallContext context)
 {
     try
     {
         var handler = new ServiceHandlerHelper(service);
         var result  = doRsfService(service, request, responseAction, context, handler);
         return(handler.ServerResponse(request, result));
     }
     catch (Exception ex)
     {
         return(ResponseHelper.Instance.GetResponse(service, request, null, ex));
     }
 }
Beispiel #12
0
 private ServiceResponse getResponseTraceData(ServiceRegistData service, ServiceResponse response)
 {
     if (response.TraceData == null)
     {
         response.TraceData = new ResponseTraceData();
     }
     response.TraceData.RequestId = XContext.Current.RequestId;
     response.TraceData.TraceId   = XContext.Current.TraceId;
     response.TraceData.RpcId     = XContext.Current.RpcId;
     response.TraceData.ClientIP  = XContext.Current.ClientIP;
     response.TraceData.Token     = XContext.Current.Token;
     response.TraceData.LogId     = XContext.Current.LogId;
     return(response);
 }
Beispiel #13
0
 private ServiceResponse doRsfService(ServiceRegistData service, ServiceRequest request, Action <object> responseAction, RpcCallContext context, ServiceHandlerHelper handler)
 {
     try
     {
         sendMsg(context, $"RPC   Start>>[{request.TraceData.RequestId}.{request.TraceData.TraceId}]>>[{request.ServiceData.ServiceInfc}].[{request.ServiceData.ServiceMethod}]");
         request = handler.ServerRequest(request);
         var result = doExcuteService(service, request, responseAction, context);
         return(ResponseHelper.Instance.GetResponse(service, request, result, null));
     }
     catch (Exception ex)
     {
         return(ResponseHelper.Instance.GetResponse(service, request, null, ex));
     }
 }
Beispiel #14
0
        private ServiceResponse errServiceResponse(ServiceRegistData service, ServiceRequest request, Exception ex)
        {
            var xex = getXException(ex);

            if (xex == null)
            {
                sendErrMsg($"RPC  Failed>>[{request.TraceData.RequestId}.{request.TraceData.TraceId}]>>[{request.ServiceData.ServiceInfc}].[{request.ServiceData.ServiceMethod}]\r\n{ex.ToString()}");
                return(FaillResponse(service, ex));
            }
            else
            {
                sendErrMsg($"RPC  Failed>>[{request.TraceData.RequestId}.{request.TraceData.TraceId}]>>[{request.ServiceData.ServiceInfc}].[{request.ServiceData.ServiceMethod}]\r\n{xex.ToString()}");
                return(FaillResponse(service, xex));
            }
        }
Beispiel #15
0
 private ServiceRequest getRequestTraceData(ServiceRegistData service, ServiceRequest request)
 {
     if (request.TraceData == null)
     {
         request.TraceData = new RequestTraceData();
     }
     request.TraceData.AppClusterId = XApp.Current.AppId;
     request.TraceData.RequestId    = Guid.NewGuid().ToString();
     request.TraceData.TraceId      = XContext.Current.TraceId;
     request.TraceData.RpcId        = XContext.Current.RpcId;
     request.TraceData.ClientIP     = XContext.Current.ClientIP;
     request.TraceData.Token        = XContext.Current.Token;
     request.TraceData.LogId        = XContext.Current.LogId;
     return(request);
 }
Beispiel #16
0
 private ServiceRequest getRequestServiceData(ServiceRegistData service, ServiceRequest request)
 {
     if (request.ServiceData == null)
     {
         request.ServiceData = new RequestServiceData();
     }
     request.ServiceData.ServiceId        = service.RegistData.ServiceId;
     request.ServiceData.ServiceProvderId = service.RegistData.ServiceProvderId;
     request.ServiceData.ServiceName      = service.RegistData.ServiceName;
     request.ServiceData.ServiceInfc      = service.RegistData.ServiceInfc;
     request.ServiceData.ServiceImpl      = service.RegistData.ServiceImpl;
     if (string.IsNullOrWhiteSpace(request.ServiceData.ServiceMethod))
     {
         request.ServiceData.ServiceMethod = service.RegistData.ServiceMethod;
     }
     return(request);
 }
Beispiel #17
0
        private ServiceResponse FaillResponse(ServiceRegistData service, Exception ex)
        {
            var result = getServiceResponse(service, null);

            result.ServiceData.ServiceCode = 99990;
            result.ServiceData.ServiceMsg  = string.Empty;
            if (ex != null)
            {
                result.ServiceData.ServiceError = ex.ToString();
                if (ex is XException)
                {
                    var h = ex as XException;
                    result.ServiceData.ServiceCode = h.ErrCode;
                    result.ServiceData.ServiceMsg  = string.Empty + h.ErrMsg;
                    if (h.ErrData != null)
                    {
                        result.ServiceData.ServiceData     = h.ErrData.ToJson();
                        result.ServiceData.ServiceDataType = h.ErrData.GetType().FullName();
                    }
                }
            }
            return(result);
        }
Beispiel #18
0
        private ServiceRegistData getServiceRegistData(RequestServiceData data)
        {
            ServiceRegistData result = null;
            var id = data.ServiceId;

            if (!string.IsNullOrWhiteSpace(id))
            {
                result = getServiceRegistDataById(id);
            }
            if (result != null)
            {
                return(result);
            }
            var name = data.ServiceName;

            if (!string.IsNullOrWhiteSpace(name))
            {
                result = getServiceRegistDataByName(name);
            }
            if (result != null)
            {
                return(result);
            }
            var infc   = data.ServiceInfc;
            var method = data.ServiceMethod;

            if (!string.IsNullOrWhiteSpace(infc) && !string.IsNullOrWhiteSpace(method))
            {
                result = getServiceRegistDataByMethod(infc, method);
            }
            if (result != null)
            {
                return(result);
            }
            result = getServiceRegistDataByInfc(infc);
            return(result);
        }
Beispiel #19
0
        /// <summary>
        /// 服务执行
        /// </summary>
        /// <param name="metadata"></param>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public object Excute(ServiceRegistData service, ServiceRequest request, Action <object> responseAction, RpcCallContext context)
        {
            var result = doExcute(service, request, responseAction, context);

            return(result);
        }