예제 #1
0
        /// <summary>
        /// 服务追踪
        /// </summary>
        /// <param name="nodeClientContainer"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        public async override Task <ServiceCallResult> CallAsync(INodeClientContainer nodeClientContainer, ServiceCallInfo info)
        {
            string name;

            if (!string.IsNullOrEmpty(info.ServiceName) && !string.IsNullOrEmpty(info.ActionName))
            {
                name = $"Proxy:{info.ServiceName}.{info.ActionName}({info.ServiceId.ToString()}.{info.ActionId.ToString()})";
            }
            else
            {
                name = $"Proxy:{info.ServiceId.ToString()}.{info.ActionId.ToString()}";
            }

            var crossProcessBag = new Dictionary <string, string>();

            if (ServiceContext.Current == null)
            {
                using (var trace = new StartClientTrace(name))
                {
                    TraceContextPropagation.PropagateTraceIdOnto(crossProcessBag);
                    return(await CallNext(nodeClientContainer, info, crossProcessBag, trace));
                }
            }
            else
            {
                using (var trace = new LocalTrace(name))
                {
                    TraceContextPropagation.PropagateTraceIdOnto(crossProcessBag);
                    return(await CallNext(nodeClientContainer, info, crossProcessBag, trace));
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 调用服务
        /// </summary>
        /// <param name="nodeClientContainer">NodeClient容器</param>
        /// <param name="info">服务调用信息</param>
        /// <returns></returns>
        public async override Task <ServiceCallResult> CallAsync(INodeClientContainer nodeClientContainer, ServiceCallInfo info)
        {
            if (Next == null)
            {
                return(null);
            }

            int callCount = 0;
            NetworkException exception;

            do
            {
                try
                {
                    return(await Next.CallAsync(nodeClientContainer, info));
                }
                catch (NetworkException ex)
                {
                    callCount++;
                    exception = ex;
                }
            } while (callCount < nodeClientContainer.Count);

            throw exception;
        }
예제 #3
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="proxyName">服务名称</param>
 /// <param name="serviceInfos">服务配置信息</param>
 /// <param name="serviceCaller">服务调用器对象,默认为DefaultServiceCaller</param>
 /// <param name="nodeClientContainer">NodeClient容器,默认为DefaultNodeClientContainer</param>
 public ServiceProxy(string proxyName,
                     IList <ServiceInfo> serviceInfos         = null,
                     IServiceCaller serviceCaller             = null,
                     INodeClientContainer nodeClientContainer = null)
 {
     ProxyName                = proxyName;
     logger                   = LoggerManager.ClientLoggerFactory.CreateLogger <ServiceProxy>();
     this.serviceInfos        = serviceInfos;
     this.serviceCaller       = serviceCaller ?? new DefaultServiceCaller();
     this.nodeClientContainer = nodeClientContainer ?? new DefaultNodeClientContainer();
 }
예제 #4
0
        /// <summary>
        /// 调用服务
        /// </summary>
        /// <param name="nodeClientContainer">NodeClient容器</param>
        /// <param name="info">服务调用信息</param>
        /// <returns></returns>
        public override Task <ServiceCallResult> CallAsync(INodeClientContainer nodeClientContainer, ServiceCallInfo info)
        {
            var nodeClient = nodeClientContainer.Get(info.ServiceId, info.ActionId, info.ParamList, info.ReturnType, info.Attachments);

            if (nodeClient == null)
            {
                throw new InvalidOperationException("Not found connected NodeClient.");
            }

            return(nodeClient.CallServiceAsync(info.ServiceId, info.ActionId, info.ParamList, info.ReturnType, info.Timeout, info.Attachments));
        }
예제 #5
0
        private async Task <ServiceCallResult> CallNext(INodeClientContainer nodeClientContainer, ServiceCallInfo info, Dictionary <string, string> crossProcessBag, ITrace trace)
        {
            if (info.Attachments == null)
            {
                info.Attachments = new Dictionary <string, byte[]>();
            }

            info.Attachments.Add(Constants.CrossProcessBagKey, await serializer.SerializeAsync(crossProcessBag));

            try
            {
                return(await Next.CallAsync(nodeClientContainer, info));
            }
            catch (Exception ex)
            {
                trace.AnnotateWith(PredefinedTag.Error, ex.Message);
                throw ex;
            }
        }
예제 #6
0
 /// <summary>
 /// 服务调用
 /// </summary>
 /// <param name="nodeClientContainer">NodeClient容器</param>
 /// <param name="info">服务调用信息</param>
 /// <returns></returns>
 public abstract Task <ServiceCallResult> CallAsync(INodeClientContainer nodeClientContainer, ServiceCallInfo info);