private void AddNewCaller(Type serviceType, HttpApiMethod httpApi) { //将方法添加到字典 var callerInfo = new HttpCallerInfo { CacheTime = httpApi.CacheTime, Service = serviceType, Method = httpApi.Method, TypeString = httpApi.Method.ReturnType == typeof(string), Description = httpApi.Description, Authorized = httpApi.Authorized, AuthParameter = httpApi.AuthParameter, HttpMethod = httpApi.HttpMethod }; string fullName = httpApi.Name; if (callers.ContainsKey(fullName)) { //处理重复的方法 for (int i = 0; i < 10000; i++) { var name = fullName + (i + 1); if (!callers.ContainsKey(name)) { fullName = name; break; } } } callerInfo.CallerName = fullName; callers[fullName] = callerInfo; }
/// <summary> /// 从类型中读取 /// </summary> /// <param name="interfaceType"></param> /// <param name="apiService"></param> /// <returns></returns> private IList <HttpApiMethod> ReadFromType(Type interfaceType, HttpApiService apiService) { var list = new List <HttpApiMethod>(); var serviceName = apiService.Name; var description = apiService.Description; if (string.IsNullOrEmpty(serviceName)) { serviceName = interfaceType.Name.ToLower(); if (serviceName.EndsWith("service")) { serviceName = serviceName.Substring(1, serviceName.LastIndexOf("service") - 1); } else { serviceName = serviceName.Substring(1); } } //获取类型中的方法信息 foreach (var method in CoreHelper.GetMethodsFromType(interfaceType)) { var items = apiService.ApiItems.Where(p => !string.IsNullOrEmpty(p.FullName)).ToList(); var item = items.FirstOrDefault(p => string.Compare(p.FullName, method.ToString(), true) == 0); if (item == null) { //跟方法名称比较 item = apiService.ApiItems.FirstOrDefault(p => string.Compare(p.FullName, method.Name, true) == 0); } //判断指定的服务是否在配置文件列表中 if (item != null) { var methodName = item.Name; if (string.IsNullOrEmpty(methodName)) { methodName = method.Name.ToLower(); } //判断认证参数是否存在 var exists = method.GetParameters().Any(p => string.Compare(p.Name, item.AuthParameter, true) == 0); //实例化HttpApi方法 var httpApi = new HttpApiMethod(method) { Name = string.Format("{0}.{1}", serviceName, methodName), Description = description == null ? item.Description : string.Format("【{0}】{1}", description, item.Description), Authorized = exists ? item.Authorized : false, AuthParameter = exists ? item.AuthParameter : null, CacheTime = item.CacheTime, HttpMethod = item.HttpMethod, }; list.Add(httpApi); } } return(list); }
/// <summary> /// 从类型中读取 /// </summary> /// <param name="interfaceType"></param> /// <param name="apiService"></param> /// <returns></returns> private IList<HttpApiMethod> ReadFromType(Type interfaceType, HttpApiService apiService) { var list = new List<HttpApiMethod>(); var serviceName = apiService.Name; var description = apiService.Description; if (string.IsNullOrEmpty(serviceName)) { serviceName = interfaceType.Name.ToLower(); if (serviceName.EndsWith("service")) serviceName = serviceName.Substring(1, serviceName.LastIndexOf("service") - 1); else serviceName = serviceName.Substring(1); } //获取类型中的方法信息 foreach (var method in CoreHelper.GetMethodsFromType(interfaceType)) { var items = apiService.ApiItems.Where(p => !string.IsNullOrEmpty(p.FullName)).ToList(); var item = items.FirstOrDefault(p => string.Compare(p.FullName, method.ToString(), true) == 0); if (item == null) { //跟方法名称比较 item = apiService.ApiItems.FirstOrDefault(p => string.Compare(p.FullName, method.Name, true) == 0); } //判断指定的服务是否在配置文件列表中 if (item != null) { var methodName = item.Name; if (string.IsNullOrEmpty(methodName)) { methodName = method.Name.ToLower(); } //判断认证参数是否存在 var exists = method.GetParameters().Any(p => string.Compare(p.Name, item.AuthParameter, true) == 0); //实例化HttpApi方法 var httpApi = new HttpApiMethod(method) { Name = string.Format("{0}.{1}", serviceName, methodName), Description = description == null ? item.Description : string.Format("【{0}】{1}", description, item.Description), Authorized = exists ? item.Authorized : false, AuthParameter = exists ? item.AuthParameter : null, CacheTime = item.CacheTime, HttpMethod = item.HttpMethod, }; list.Add(httpApi); } } return list; }