Example #1
0
        /// <summary>
        /// get result of task
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        private static object GetTaskResult(Task task)
        {
            try
            {
                var type = task.GetType();
                var func = GetTaskResultFuncs.GetOrAdd(type, (Type key) =>
                {
                    var arg        = Expression.Parameter(typeof(object), "task");
                    var argConvert = Expression.Convert(arg, type);
                    var expProp    = Expression.Property(argConvert, "Result");
                    var expConvert = Expression.Convert(expProp, typeof(object));
                    var asignExp   = Expression.Lambda(expConvert, arg);
                    var getFunc    = asignExp.Compile();
                    return((Func <object, object>)getFunc);
                });

                var value = func?.Invoke(task);
                return(value);
            }
            catch (Exception ex)
            {
                LogHelper.Error("GetTaskResult error", ex);
                var aex = ex as AggregateException;
                if (aex?.InnerException != null)
                {
                    throw aex.InnerException;
                }
                throw;
            }
        }
Example #2
0
        private ServiceInfo[] GetAddressInternal(string name, string group)
        {
            var itemKey = new ServiceIdentifier(name, group);
            // ReSharper disable once InconsistentlySynchronizedField
            var url = _defaultBaseUrlDictionary.GetOrAdd(itemKey, key =>
            {
                if (key == ServiceIdentifier.Empty)
                {
                    return(null);
                }
                if (_registryClient.Value == null)
                {
                    return(null);
                }

                var request = new GetServiceInfoRequest
                {
                    Services = new[]
                    {
                        new ServiceIdentifierDto
                        {
                            Name  = name,
                            Group = group,
                        }
                    }
                };
                try
                {
                    var response     = _registryClient.Value.GetServiceInfo(request);
                    var result       = response.Services.FirstOrDefault();
                    var serviceInfos = result?.ServiceInfos
                                       .Select(it => new ServiceInfo
                    {
                        Name    = name,
                        Group   = group,
                        Address = it.Address,
                        Data    = it.Data,
                    })
                                       .ToArray();

                    _updateTimeDic.AddOrUpdate(itemKey, DateTime.Now, (k, oldValue) => DateTime.Now);

                    return(serviceInfos);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    throw;
                }
            });

            return(url);
        }
Example #3
0
        ///// <summary>
        /////
        ///// </summary>
        ///// <param name="serviceType"></param>
        //public ActionManager(Type serviceType)
        //{
        //	_defaultServiceType = serviceType;
        //}

        /// <summary>
        ///
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="actionName"></param>
        /// <returns></returns>
        private RpcAction GetAction(Type serviceType, string actionName)
        {
            if (serviceType == null && _defaultServiceType == null)
            {
                throw new ArgumentOutOfRangeException(nameof(serviceType), "parameter serviceType can't be null when defaultServiceType is null");
            }

            serviceType = serviceType ?? _defaultServiceType;
            var actionKey = _ignoreCase
                                ? actionName.ToLower()
                                : actionName;

            return(_actions.GetOrAdd(actionKey, k => GetActionInternal(serviceType, actionName)));
        }
Example #4
0
        public static MethodInfo GetActionMethod(Type serviceType, string action, bool ignoreCase)
        {
            var methodKey = serviceType.Name + "." + action;

            return(ActionMethods.GetOrAdd(methodKey, k =>
            {
                var methods = serviceType.GetMethods();
                var method = ignoreCase
                                        ? methods
                             .FirstOrDefault(it => string.Compare(it.Name, action, StringComparison.OrdinalIgnoreCase) == 0)
                                        : methods
                             .FirstOrDefault(it => string.CompareOrdinal(it.Name, action) == 0);

                return method;
            }));
        }
Example #5
0
        public static ServiceInstanceContainer GetService(RpcAction action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }
            if (action.ServiceCreator == null)
            {
                throw new ArgumentException("action.ServiceCreator can't be null");
            }

            var container = Pool.GetOrAdd(action, (k) =>
                                          new ServiceInstanceContainerPool(action.ServiceCreator)
            {
                Size = 1000
            });

            return(container.GetInstanceContainer());
        }
Example #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="actionName"></param>
        /// <returns></returns>
        public RpcActionInfo GetAction(string actionName)
        {
            var actionKey = /*serviceType.FullName + "." + */ actionName;

            return(_actions.GetOrAdd(actionKey, k => GetActionInternal(_contractType, actionName)));
        }
Example #7
0
 public static Delegate GetCallMethodAsyncFunc(Type serviceType, Type argumentType, ParameterInfo[] arguments, MethodInfo method, bool hasReturn)
 {
     return(AsyncCallMethods.GetOrAdd(method, (k) => GetCallMethodAsyncFuncInternal(serviceType, argumentType, arguments, method, hasReturn)));
 }