Exemple #1
0
        /// <summary>
        /// 检查指定类型的服务并根据自定义属性标签在系统中进行注册
        /// </summary>
        /// <param name="type">服务类型</param>
        public void InspectService(Type type)
        {
            if (type.IsDefined(typeof(RemoteServiceAttribute), true))
            {
                // 通过服务属性标签来注册服务
                RemoteServiceAttribute serviceAttribute = type.GetCustomAttributes(typeof(RemoteServiceAttribute), true)[0] as RemoteServiceAttribute;
                string serviceKey = SecurityUtility.HashObject(type);
                RegisterService(serviceKey, type.Name,
                                serviceAttribute.Description,
                                serviceAttribute.ServiceType,
                                type,
                                serviceAttribute.ServiceScope);

                // 检查所有方法并注册
                MethodInfo[] methods = type.GetMethods();
                foreach (MethodInfo method in methods)
                {
                    MethodInfo mi = method;
                    if (method.IsGenericMethod)
                    {
                        mi = method.GetGenericMethodDefinition();
                    }

                    if (mi.IsDefined(typeof(RemoteMethodAttribute), true))
                    {
                        string dataUpdateEvent = null;
                        if (mi.IsDefined(typeof(ClientCacheAttribute), true))
                        {
                            if (mi.ReturnType == typeof(void))
                            {
                                logger.Warn("方法 [" + mi.Name + "] 定义了 [ClientCacheAttribute] 特性,但是却没有返回值,已被忽略。");
                            }
                            else
                            {
                                ClientCacheAttribute cacheAttribute = mi.GetCustomAttributes(typeof(ClientCacheAttribute), true)[0] as ClientCacheAttribute;
                                dataUpdateEvent = cacheAttribute.DataUpdateEvent;
                            }
                        }
                        // 注册方法
                        RemoteMethodAttribute rmAttribute = mi.GetCustomAttributes(typeof(RemoteMethodAttribute), true)[0] as RemoteMethodAttribute;
                        RegisterFunction(SecurityUtility.HashObject(mi),
                                         serviceKey,
                                         mi.Name,
                                         rmAttribute.Description,
                                         rmAttribute.Offline,
                                         mi,
                                         dataUpdateEvent);
                    }
                    else if (method.IsDefined(typeof(ClientCacheAttribute), true))
                    {
                        logger.Warn("方法 [" + method.Name + "] 定义了 [ClientCacheAttribute] 特性,但是却没有定义 [RemoteMethodAttribute],已被忽略。");
                    }
                }
            }
        }
Exemple #2
0
        private static bool IsApiExplorerDisabled(DynamicApiControllerInfo dynamicApiControllerInfo)
        {
            if (dynamicApiControllerInfo.IsApiExplorerEnabled == false)
            {
                if (!RemoteServiceAttribute.IsMetadataExplicitlyEnabledFor(dynamicApiControllerInfo.ServiceInterfaceType))
                {
                    return(true);
                }
            }
            else
            {
                if (RemoteServiceAttribute.IsMetadataExplicitlyDisabledFor(dynamicApiControllerInfo.ServiceInterfaceType))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #3
0
        private static bool IsApiExplorerDisabled(DynamicApiActionInfo dynamicApiActionInfo)
        {
            if (dynamicApiActionInfo.IsApiExplorerEnabled == false)
            {
                if (!RemoteServiceAttribute.IsMetadataExplicitlyEnabledFor(dynamicApiActionInfo.Method))
                {
                    return(true);
                }
            }
            else
            {
                if (RemoteServiceAttribute.IsMetadataExplicitlyDisabledFor(dynamicApiActionInfo.Method))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #4
0
        public void Build()
        {
            var types =
                from
                type in assembly.GetTypes()
                where
                (type.IsPublic || type.IsNestedPublic) &&
                type.IsInterface &&
                typeof(T).IsAssignableFrom(type) &&
                iocResolver.IsRegistered(type) &&
                !RemoteServiceAttribute.IsExplicitlyDisabledFor(type)
                select
                type;

            if (typePredicate != null)
            {
                types = types.Where(t => typePredicate(t));
            }

            foreach (var type in types)
            {
                var serviceName = serviceNameSelector != null
                    ? serviceNameSelector(type)
                    : GetConventionalServiceName(type);

                if (!string.IsNullOrWhiteSpace(servicePrefix))
                {
                    serviceName = servicePrefix + "/" + serviceName;
                }

                var builder = typeof(IDynamicApiControllerBuilder)
                              .GetMethod("For", BindingFlags.Public | BindingFlags.Instance)
                              .MakeGenericMethod(type)
                              .Invoke(dynamicApiControllerBuilder, new object[] { serviceName });

                if (filters != null)
                {
                    builder.GetType()
                    .GetMethod("WithFilters", BindingFlags.Public | BindingFlags.Instance)
                    .Invoke(builder, new object[] { filters });
                }

                if (isApiExplorerEnabled != null)
                {
                    builder.GetType()
                    .GetMethod("WithApiExplorer", BindingFlags.Public | BindingFlags.Instance)
                    .Invoke(builder, new object[] { isApiExplorerEnabled });
                }

                if (isProxyScriptingEnabled != null)
                {
                    builder.GetType()
                    .GetMethod("WithProxyScripts", BindingFlags.Public | BindingFlags.Instance)
                    .Invoke(builder, new object[] { isProxyScriptingEnabled.Value });
                }

                if (conventionalVerbs)
                {
                    builder.GetType()
                    .GetMethod("WithConventionalVerbs", BindingFlags.Public | BindingFlags.Instance)
                    .Invoke(builder, new object[0]);
                }

                if (forMethodsAction != null)
                {
                    builder.GetType()
                    .GetMethod("ForMethods", BindingFlags.Public | BindingFlags.Instance)
                    .Invoke(builder, new object[] { forMethodsAction });
                }

                builder.GetType()
                .GetMethod("Build", BindingFlags.Public | BindingFlags.Instance)
                .Invoke(builder, new object[0]);
            }
        }
Exemple #5
0
        public void Build()
        {
            var types =
                from
                type in _assembly.GetTypes()
                where
                (type.IsPublic || type.IsNestedPublic) &&
                type.IsInterface &&
                typeof(T).IsAssignableFrom(type) &&
                IocManager.Instance.IsRegistered(type) &&
                !RemoteServiceAttribute.IsExplicitlyDisabledFor(type)
                select
                type;

            if (_typePredicate != null)
            {
                types = types.Where(t => _typePredicate(t));
            }

            foreach (var type in types)
            {
                var serviceName = _serviceNameSelector != null
                    ? _serviceNameSelector(type)
                    : GetConventionalServiceName(type);

                if (!string.IsNullOrWhiteSpace(_servicePrefix))
                {
                    serviceName = _servicePrefix + "/" + serviceName;
                }

                var builder = typeof(DynamicApiControllerBuilder)
                              .GetMethod("For", BindingFlags.Public | BindingFlags.Static)
                              .MakeGenericMethod(type)
                              .Invoke(null, new object[] { serviceName });

                if (_filters != null)
                {
                    builder.GetType()
                    .GetMethod("WithFilters", BindingFlags.Public | BindingFlags.Instance)
                    .Invoke(builder, new object[] { _filters });
                }

                if (_conventionalVerbs)
                {
                    builder.GetType()
                    .GetMethod("WithConventionalVerbs", BindingFlags.Public | BindingFlags.Instance)
                    .Invoke(builder, new object[0]);
                }

                if (_forMethodsAction != null)
                {
                    builder.GetType()
                    .GetMethod("ForMethods", BindingFlags.Public | BindingFlags.Instance)
                    .Invoke(builder, new object[] { _forMethodsAction });
                }

                builder.GetType()
                .GetMethod("Build", BindingFlags.Public | BindingFlags.Instance)
                .Invoke(builder, new object[0]);
            }
        }
        private static bool TryGetRemoteService(ServiceDescriptor serverTypeDesc, SchubertOptions options, out RemoteServiceAttribute attribute)
        {
            if (serverTypeDesc.ServiceType.TryGetRemoteServiceAttribute(true, out attribute))
            {
                if (!attribute.VipAddress.CaseInsensitiveEquals($"{options.Group}.{options.AppSystemName}"))
                {
                    var svcType = serverTypeDesc.ServiceType.GetTypeInfo();

                    String optionsName = nameof(SchubertOptions);
                    throw new SwiftyApplicationException($"{nameof(RemoteServiceAttribute)} 的属性" +
                                                         $" {nameof(RemoteServiceAttribute.VipAddress)} 必须使用 [{optionsName}.{nameof(SchubertOptions.Group)}].[{optionsName}.{nameof(SchubertOptions.AppSystemName)}] (区分大小写)格式。{System.Environment.NewLine}" +
                                                         $"类型 '{svcType.FullName}' 或程序集 '{svcType.Assembly.FullName}' 上的 {nameof(RemoteServiceAttribute)} 不满足该要求。");
                }

                return(true);
            }
            return(false);
        }