Пример #1
0
        /// <summary>
        /// 远程调用。
        /// </summary>
        /// <typeparam name="T">返回类型。</typeparam>
        /// <param name="parameters">参数字典。</param>
        /// <param name="serviceId">服务Id。</param>
        /// <returns>调用结果。</returns>
        protected async Task <T> InvokeAsync <T>(IDictionary <string, object> parameters, string serviceId)
        {
            if (_endPoint == null)
            {
                throw new RpcException($"无法解析服务Id:{serviceId}的地址信息。");
            }

            var message = await _remoteInvokeService.InvokeAsync(new RemoteInvokeContext
            {
                InvokeMessage = new RemoteInvokeMessage
                {
                    Parameters = parameters,
                    ServiceId  = serviceId
                }
            }, _endPoint);

            if (message == null)
            {
                return(default(T));
            }

            var result = _typeConvertibleService.Convert(message.Result, typeof(T));

            return((T)result);
        }
Пример #2
0
        /// <summary>
        /// 远程调用。
        /// </summary>
        /// <typeparam name="T">返回类型。</typeparam>
        /// <param name="parameters">参数字典。</param>
        /// <param name="serviceId">服务Id。</param>
        /// <returns>调用结果。</returns>
        protected async Task <T> Invoke <T>(IDictionary <string, object> parameters, string serviceId)
        {
            var message = await _remoteInvokeService.InvokeAsync(new RemoteInvokeContext
            {
                InvokeMessage = new RemoteInvokeMessage
                {
                    Parameters = parameters,
                    ServiceId  = serviceId
                }
            });

            var result = _typeConvertibleService.Convert(message.Result, typeof(T));

            return((T)result);
        }
        private async Task <RemoteInvokeResultMessage> MonitorRemoteInvokeAsync(IDictionary <string, object> parameters, string serviceId, string serviceKey, bool decodeJOject, int requestTimeout, string item)
        {
            CancellationTokenSource source = new CancellationTokenSource();
            var token         = source.Token;
            var invokeMessage = new RemoteInvokeMessage
            {
                Parameters   = parameters,
                ServiceId    = serviceId,
                ServiceKey   = serviceKey,
                DecodeJOject = decodeJOject,
                Attachments  = RpcContext.GetContext().GetContextParameters()
            };

            try
            {
                _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                {
                    v.RemoteServiceRequests = v.RemoteServiceRequests == null ? 1 : ++v.RemoteServiceRequests;
                    v.FinalRemoteInvokeTime = DateTime.Now;
                    ++v.ConcurrentRequests;
                    return(v);
                });
                var message = await _remoteInvokeService.InvokeAsync(new RemoteInvokeContext
                {
                    Item          = item,
                    InvokeMessage = invokeMessage
                }, requestTimeout);

                _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                {
                    v.SinceFaultRemoteServiceRequests = 0;
                    --v.ConcurrentRequests; return(v);
                });
                return(message);
            }
            catch (Exception ex)
            {
                _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                {
                    ++v.FaultRemoteServiceRequests;
                    ++v.SinceFaultRemoteServiceRequests;
                    --v.ConcurrentRequests;
                    return(v);
                });
                await ExecuteExceptionFilter(ex, invokeMessage, token);

                if (ex is BusinessException || ex.InnerException is BusinessException)
                {
                    return(new RemoteInvokeResultMessage()
                    {
                        ExceptionMessage = ex.GetExceptionMessage(),
                        Result = null,
                        StatusCode = ex.GetGetExceptionStatusCode()
                    });
                }

                return(null);
            }
        }
        private async Task <RemoteInvokeResultMessage> MonitorRemoteInvokeAsync(IDictionary <string, object> parameters, string serviceId, string serviceKey, bool decodeJOject, int requestTimeout, int hashCode)
        {
            var serviceInvokeInfo = _serviceInvokeListenInfo.GetOrAdd(serviceId, new ServiceInvokeListenInfo());

            try
            {
                _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                {
                    v.RemoteServiceRequests = v.RemoteServiceRequests == null ? 1 : ++v.RemoteServiceRequests;
                    v.FinalRemoteInvokeTime = DateTime.Now;
                    ++v.ConcurrentRequests;
                    return(v);
                });
                var message = await _remoteInvokeService.InvokeAsync(new RemoteInvokeContext
                {
                    HashCode      = hashCode,
                    InvokeMessage = new RemoteInvokeMessage
                    {
                        Parameters   = parameters,
                        ServiceId    = serviceId,
                        ServiceKey   = serviceKey,
                        DecodeJOject = decodeJOject,
                        Attachments  = RpcContext.GetContext().GetContextParameters()
                    }
                }, requestTimeout);

                _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                {
                    v.SinceFaultRemoteServiceRequests = 0;
                    --v.ConcurrentRequests; return(v);
                });
                return(message);
            }
            catch
            {
                _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                {
                    ++v.FaultRemoteServiceRequests;
                    ++v.SinceFaultRemoteServiceRequests;
                    --v.ConcurrentRequests;
                    return(v);
                });
                return(null);
            }
        }
Пример #5
0
        /// <summary>
        /// 远程调用。
        /// </summary>
        /// <typeparam name="T">返回类型。</typeparam>
        /// <param name="parameters">参数字典。</param>
        /// <param name="serviceId">服务Id。</param>
        /// <param name="timeout">超时时间,单位秒</param>
        /// <returns>调用结果。</returns>
        protected async Task <T> InvokeAsync <T>(IDictionary <string, object> parameters, string serviceId, int timeout = 60)
        {
            if (_endPoint == null)
            {
                throw new RpcException($"无法解析服务Id:{serviceId}的地址信息。");
            }

            var message = await _remoteInvokeService.InvokeAsync(new RemoteInvokeContext
            {
                InvokeMessage = new RemoteInvokeMessage
                {
                    Parameters = parameters,
                    ServiceId  = serviceId
                }
            }, _endPoint, timeout);

            if (message == null)
            {
                return(default);
Пример #6
0
        private async Task <RemoteInvokeResultMessage> MonitorRemoteInvokeAsync(IDictionary <string, object> parameters, string serviceId, string serviceKey)
        {
            var serviceInvokeInfo = _serviceInvokeListenInfo.GetOrAdd(serviceId, new ServiceInvokeListenInfo());

            try
            {
                _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                {
                    v.RemoteServiceRequests = v.RemoteServiceRequests == null ? 1 : ++v.RemoteServiceRequests;
                    v.FinalRemoteInvokeTime = DateTime.Now;
                    ++v.ConcurrentRequests;
                    return(v);
                });
                var message = await _remoteInvokeService.InvokeAsync(new RemoteInvokeContext
                {
                    InvokeMessage = new RemoteInvokeMessage
                    {
                        Parameters = parameters,
                        ServiceId  = serviceId,
                        ServiceKey = serviceKey
                    }
                });

                _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                {
                    v.SinceFaultRemoteServiceRequests = 0;
                    --v.ConcurrentRequests; return(v);
                });

                return(message);
            }
            catch
            {
                _serviceInvokeListenInfo.AddOrUpdate(serviceId, new ServiceInvokeListenInfo(), (k, v) =>
                {
                    ++v.FaultRemoteServiceRequests;
                    ++v.SinceFaultRemoteServiceRequests;
                    --v.ConcurrentRequests;
                    return(v);
                });
                return(null);
            }
        }