コード例 #1
0
        private void AddHttpServiceRouter(Type type, MethodInfo m, RpcServiceAttribute sAttr, RpcMethodAttribute mAttr,
                                          RouterAttribute rAttr, HttpRouteOptions options)
        {
            var item = new RouteItem
            {
                Path         = rAttr.Path,
                AcceptVerb   = rAttr.AcceptVerb,
                Category     = rAttr.Category,
                InvokeMethod = m,
                MessageId    = mAttr.MessageId,
                ServiceId    = sAttr.ServiceId
            };

            //special MessageId;
            var args = new object[] { this.specialMessageId };

            item.InvokeService = this._proxyCreate.MakeGenericMethod(type).Invoke(this._proxy, args);

            if (rAttr.PluginType != null)
            {
                item.Plugin = ActivatorUtilities.CreateInstance(this._provider, rAttr.PluginType) as IHttpPlugin;
            }
            options.Items.Add(item);

            _logger.LogDebug("url:{0},verb:{1},service:{2},method:{3}",
                             item.Path, item.AcceptVerb, type.Name.Split('.').Last(), m.Name);
        }
コード例 #2
0
        private void AddRpcService(Type type, RpcServiceAttribute sAttr, HttpRouteOptions options, params string[] categories)
        {
            var methods = type.GetMethods();

            foreach (var m in methods)
            {
                var mAttr = m.GetCustomAttribute <RpcMethodAttribute>();
                if (mAttr == null)
                {
                    continue;
                }

                var rAttr = m.GetCustomAttribute <RouterAttribute>();
                if (rAttr == null)
                {
                    continue;
                }

                if (categories != null && categories.Any())
                {
                    if (categories.Contains(rAttr.Category))
                    {
                        AddHttpServiceRouter(type, m, sAttr, mAttr, rAttr, options);
                    }
                }
                else
                {
                    if ("default".Equals(rAttr.Category, StringComparison.OrdinalIgnoreCase))
                    {
                        AddHttpServiceRouter(type, m, sAttr, mAttr, rAttr, options);
                    }
                }
            }
        }
コード例 #3
0
ファイル: RpcClientInterface.cs プロジェクト: ilahsa/bai_pro
        public RpcClientInterface(Type intf)
        {
            if (!intf.IsInterface)
            {
                throw new NotSupportedException();
            }

            RpcServiceAttribute serviceAttr = AttributeHelper.GetAttribute <RpcServiceAttribute>(intf);

            ServiceName    = serviceAttr.ServiceName;
            ClientCheck    = serviceAttr.ClientChecking;
            EnableCounters = serviceAttr.EnableCounters;
            _methods       = new HybridDictionary <string, RpcClientMethodSensor>();

            foreach (MethodInfo method in intf.GetMethods())
            {
                RpcClientMethodSensor m;
                string methodName = method.Name;

                RpcServiceBatchMethodAttribute battr = AttributeHelper.TryGetAttribute <RpcServiceBatchMethodAttribute>(method);
                if (battr != null)
                {
                    if (!string.IsNullOrEmpty(battr.MethodName))
                    {
                        methodName = battr.MethodName;
                    }

                    m = new RpcClientMethodSensor()
                    {
                        ArgsType     = battr.ArgsType,
                        BatchManager = new RpcClientBatchManager(battr.BatchCount, battr.IdleMs)
                    };
                }
                else
                {
                    RpcServiceMethodAttribute attr = AttributeHelper.GetAttribute <RpcServiceMethodAttribute>(method);

                    if (!string.IsNullOrEmpty(attr.MethodName))
                    {
                        methodName = battr.MethodName;
                    }

                    m = new RpcClientMethodSensor()
                    {
                        ArgsType     = attr.ArgsType,
                        BatchManager = null
                    };
                }
                _methods.Add(methodName, m);
            }
        }
コード例 #4
0
        public object InvokeRemoteMethod(IInvocation invocation, byte serializeType)
        {
            string     serviceName = invocation.Method.DeclaringType.Name.TrimStart('I');
            string     messageName = invocation.Method.Name;
            var        arguments   = invocation.Arguments;
            object     firstP      = arguments.Length > 0 ? arguments[0] : null;
            MethodInfo method      = invocation.Method;
            Type       returnType  = method.ReturnType;

            RpcServiceAttribute rpcSericeAttr = invocation.Method.DeclaringType.GetCustomAttribute <RpcServiceAttribute>(true);
            var timeout = rpcSericeAttr != null && rpcSericeAttr.Timeout > 0 ? rpcSericeAttr.Timeout : _clientOptions.Timeout; //可以从接口特性上解析,没有配置才取默认配置

            return(InvokeRemoteMethod(serviceName, invocation.Method, firstP, timeout, serializeType));
        }
コード例 #5
0
ファイル: LocalCacheService.cs プロジェクト: hsdotnet/rpc
        private void InitServiceMetadata()
        {
            Type[] types = ReflectionHelper.GetAssembly(_section.ServiceDLL).GetTypes();

            foreach (Type type in types)
            {
                if (!type.IsInterface)
                {
                    continue;
                }

                RpcServiceAttribute serviceAttribute = (RpcServiceAttribute)type.GetCustomAttribute(typeof(RpcServiceAttribute), false);
                if (serviceAttribute != null)
                {
                    ServiceMetadata serviceMetadata = new ServiceMetadata()
                    {
                        ServiceName     = type.FullName,
                        ServiceType     = type,
                        ServiceImplType = ReflectionHelper.GetServiceImplType(types, type),
                    };

                    foreach (MethodInfo method in type.GetMembers())
                    {
                        RpcMethodAttribute methodAttribute = (RpcMethodAttribute)method.GetCustomAttribute(typeof(RpcMethodAttribute), false);
                        if (methodAttribute != null)
                        {
                            MethodMetadata methodMetadata = new MethodMetadata()
                            {
                                MethodName = methodAttribute.MethodName,
                                Method     = method
                            };

                            serviceMetadata.MethodMetadatas.Add(methodMetadata);
                        }
                    }

                    _cacheContainer.Application.ServiceMetadatas.Add(serviceMetadata);
                }
            }
        }
コード例 #6
0
        public static void Initialize()
        {
            try
            {
                string bastPath    = AppDomain.CurrentDomain.BaseDirectory;
                var    serviceDlls = ConfigurationManager.AppSettings["rpc.service"].Split(',').ToList();
                if (serviceDlls.Count == 0)
                {
                    throw new NotExistException("没有找到服务实现");
                }

                foreach (var dllPath in serviceDlls)
                {
                    Assembly    assembly = Assembly.LoadFrom(bastPath + "/" + dllPath);
                    List <Type> types    = assembly.GetTypes().ToList();
                    foreach (var type in types)
                    {
                        RpcServiceAttribute attribute = type.GetCustomAttribute <RpcServiceAttribute>();
                        if (attribute != null)
                        {
                            List <Type> interfaces = type.GetInterfaces().ToList();
                            foreach (var iInterface in interfaces)
                            {
                                RpcServiceAttribute attribute1 = iInterface.GetCustomAttribute <RpcServiceAttribute>();
                                if (attribute1 != null)
                                {
                                    ServiceContainer.Add(iInterface.FullName, Activator.CreateInstance(type));
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                throw;
            }
        }
コード例 #7
0
        private static RpcServiceInfo GetServiceInfoFromRpcAttribute(Type serviceType, Type?implementationType, RpcServiceAttribute rpcAttribute)
        {
            string serviceName;
            string serviceNamespace;

            // Try to retrieve it from the server side definition
            if (rpcAttribute.ServerDefinitionType != null)
            {
                if (GetRpcServiceAttribute(rpcAttribute.ServerDefinitionType) is RpcServiceAttribute serverRpcAttribute)
                {
                    serviceName      = GetServiceName(rpcAttribute.ServerDefinitionType, serverRpcAttribute);
                    serviceNamespace = GetServiceNamespace(rpcAttribute.ServerDefinitionType, serverRpcAttribute);
                }
                else if (GetServiceContractAttribute(rpcAttribute.ServerDefinitionType) is Attribute contractAttribute)
                {
                    serviceName      = GetServiceName(rpcAttribute.ServerDefinitionType, contractAttribute);
                    serviceNamespace = rpcAttribute.ServerDefinitionType.Namespace ?? "";
                }
                else
                {
                    throw new RpcDefinitionException("Server side definition interface must be tagged with the RpcService or ServiceContract attribute.");
                }
            }
            else
            {
                serviceName      = GetServiceName(serviceType, rpcAttribute);
                serviceNamespace = GetServiceNamespace(serviceType, rpcAttribute);
            }

            if (!string.IsNullOrEmpty(rpcAttribute.Name) && rpcAttribute.Name != serviceName)
            {
                throw new RpcDefinitionException("Name of server side type does not match specified service name.");;
            }

            if (!string.IsNullOrEmpty(rpcAttribute.Namespace) && rpcAttribute.Namespace != serviceNamespace)
            {
                throw new RpcDefinitionException("Namespace of server side type does not match specified service namespace.");;
            }


            var definitionType = rpcAttribute.ServiceDefinitionSide;

            var metadata = new List <object>();

            metadata.AddRange(serviceType.GetCustomAttributes(inherit: true));
            if (implementationType != null)
            {
                metadata.AddRange(implementationType.GetCustomAttributes(inherit: true));
            }

            return(new RpcServiceInfo
                   (
                       type: serviceType,
                       @namespace: serviceNamespace,
                       name: serviceName,
                       definitionSide: definitionType,
                       serverType: rpcAttribute.ServerDefinitionType,
                       implementationType: implementationType,
                       isSingleton: rpcAttribute.IsSingleton,
                       metadata: metadata.ToImmutableArray()
                   ));
        }