コード例 #1
0
        /// <summary>
        /// 检查方法
        /// </summary>
        /// <param name="businessKindName"></param>
        /// <param name="businessMethodName"></param>
        /// <returns></returns>
        public bool CheckAuthorized(string businessKindName, string businessMethodName)
        {
            BusinessKindModel kind = businessPool.Where(e => e.Key.Equals(businessKindName, StringComparison.OrdinalIgnoreCase)).Select(v => v.Value).SingleOrDefault();

            if (kind != null)
            {
                BusinessMethodModel method = kind.MethodModels.Where(e => e.Key.Equals(businessMethodName, StringComparison.OrdinalIgnoreCase)).Select(v => v.Value).SingleOrDefault();
                if (method != null)
                {
                    return(method.Authorized);
                }
            }

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// 查找方法
        /// </summary>
        /// <param name="businessKindName"></param>
        /// <param name="businessMethodName"></param>
        /// <returns></returns>
        public BusinessMethodModel FindMethod(string businessKindName, string businessMethodName)
        {
            BusinessKindModel   kind   = businessPool.Where(e => e.Key.Equals(businessKindName, StringComparison.OrdinalIgnoreCase)).Select(v => v.Value).SingleOrDefault();
            BusinessMethodModel method = null;

            if (kind == null)
            {
                throw new RESTfulException((int)HttpStatusCode.NotFound, businessKindName + ", did not found!");
            }
            else
            {
                method = kind.MethodModels.Where(e => e.Key.Equals(businessMethodName, StringComparison.OrdinalIgnoreCase)).Select(v => v.Value).SingleOrDefault();
                if (method == null)
                {
                    throw new RESTfulException((int)HttpStatusCode.NotFound, businessMethodName + ", did not found!");
                }
            }

            return(method);
        }
コード例 #3
0
        public void Register(IBusinessPool businessPool)
        {
            pool = businessPool;
            //读取配置文件
            try
            {
                BusinessKindModel   kindModel   = null;
                BusinessMethodModel methodModel = null;

                var container = new WindsorContainer();
                if (ConfigurationManager.GetSection("mysoft.framework/restful") != null)
                {
                    container = new WindsorContainer(new XmlInterpreter(new ConfigResource("mysoft.framework/restful")));
                }

                //给当前容器赋值
                this.container = new ServiceContainer(container);

                foreach (var type in GetInterfaceServices <PublishKindAttribute>(container))
                {
                    //获取类特性
                    var kind = CoreHelper.GetTypeAttribute <PublishKindAttribute>(type);
                    if (kind == null)
                    {
                        continue;
                    }

                    kind.Name = kind.Name.ToLower();

                    //如果包含了相同的类别,则继续
                    if (pool.KindMethods.ContainsKey(kind.Name))
                    {
                        kindModel = pool.KindMethods[kind.Name];
                    }
                    else
                    {
                        kindModel = new BusinessKindModel
                        {
                            Name        = kind.Name,
                            Description = kind.Description
                        };

                        pool.AddKindModel(kind.Name, kindModel);
                    }

                    //获取方法特性
                    foreach (MethodInfo info in CoreHelper.GetMethodsFromType(type))
                    {
                        var method = CoreHelper.GetMemberAttribute <PublishMethodAttribute>(info);
                        if (method != null)
                        {
                            method.Name = method.Name.ToLower();

                            //如果包含了相同的方法,则继续
                            if (kindModel.MethodModels.ContainsKey(method.Name))
                            {
                                //处理重复的方法
                                for (int i = 0; i < 10000; i++)
                                {
                                    var name = method.Name + (i + 1);
                                    if (!kindModel.MethodModels.ContainsKey(name))
                                    {
                                        method.Name = name;
                                        break;
                                    }
                                }
                            }

                            methodModel = new BusinessMethodModel
                            {
                                Name            = method.Name,
                                Description     = method.Description,
                                HttpMethod      = HttpMethod.GET,
                                Authorized      = method.Authorized,
                                Method          = info,
                                Parameters      = info.GetParameters(),
                                ParametersCount = info.GetParameters().Count(),
                                Service         = type
                            };

                            var types = info.GetParameters().Select(p => p.ParameterType).ToArray();
                            if (!CoreHelper.CheckPrimitiveType(types))
                            {
                                methodModel.HttpMethod = HttpMethod.POST;
                            }

                            kindModel.MethodModels.Add(method.Name, methodModel);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SimpleLog.Instance.WriteLogForDir("RESTful", ex);
            }
        }