// 注册类库 private void RegLibrary(string path) { Program.Println($" 正在注册类库 {path} ..."); // 加载类库 Assembly assembly = Assembly.LoadFile(path); // 遍历类库中的所有类 var tps = assembly.GetTypes(); foreach (var tp in tps) { // 遍历类型特性,判断类是否满足条件 var attrs = tp.GetCustomAttributes(); foreach (var attr in attrs) { if (attr.GetType().FullName == "dpz3.Modular.ModularAttribute") { // 重建模块化特性 dpz3.Modular.ModularAttribute modular = (dpz3.Modular.ModularAttribute)attr; // 找到Api定义类 switch (modular.ModularType) { case ModularTypes.Api: case ModularTypes.Session: case ModularTypes.SessionApi: // 读取路由地址定义 string route = modular.Route.Replace("{ControllerName}", tp.Name); // 确定类名称 string className = ""; switch (modular.ModularType) { case ModularTypes.Api: className = "ControllerBase"; break; case ModularTypes.Session: className = "SessionControllerBase"; break; case ModularTypes.SessionApi: className = "JttpSessionControllerBase"; break; } // 遍历所有的函数定义 var methods = tp.GetMethods(); foreach (var method in methods) { // 遍历函数特性,判断函数是否满足条件 var methodAttrs = method.GetCustomAttributes(); foreach (var methodAttr in methodAttrs) { if (methodAttr.GetType().FullName == "dpz3.Modular.ModularAttribute") { // 重建模块化特性 dpz3.Modular.ModularAttribute methodModular = (dpz3.Modular.ModularAttribute)methodAttr; string routeNew = $"{route}/{methodModular.Route}".ToLower(); ModularMethodInfo methodInfo = new ModularMethodInfo() { Method = method, Assembly = assembly, Type = tp, Route = routeNew, ClassName = className, }; switch (methodModular.ModularType) { case dpz3.Modular.ModularTypes.Post: // 添加一条POST接口信息 Program.Println($"[@] POST /{this.Name}{routeNew} ..."); Posts.Add(methodInfo); break; case dpz3.Modular.ModularTypes.Get: // 添加一条GET接口信息 Program.Println($"[@] GET /{this.Name}{routeNew} ..."); Gets.Add(methodInfo); // 处理特殊的首页 if (routeNew.EndsWith("/index")) { routeNew = routeNew.Substring(0, routeNew.Length - 5); // 添加一条GET接口信息 Program.Println($"[@] GET /{this.Name}{routeNew} ..."); Gets.Add(new ModularMethodInfo() { Method = method, Assembly = assembly, Type = tp, Route = routeNew, ClassName = className, }); } break; } // 结束特性循环 break; } } } break; } // 结束特性循环 break; } } } }
// 执行接口 private Task ExecuteMethod(HttpContext httpContext, ModularMethodInfo info) { try { // 设置字符编码 httpContext.Response.ContentType = "text/plain;charset=UTF-8"; // 建立宿主对象 using (ModularHost host = new ModularHost()) { host.Version = it.Version; host.WorkFolder = it.WorkPath; host.StorageFolder = $"{it.WorkPath}storage"; host.PackageName = this.Name; host.PackageVersion = this.Version; host.PackageWorkFolder = this.WorkPath; host.Context = httpContext; if (!dpz3.Object.IsNull(it.Database.Entity)) { host.Connection = new Connection(it.Database.Entity); } string returnType = info.Method.ReturnType.FullName; switch (returnType) { case "System.String": // 处理返回为字符串的函数定义 if (info.ClassName == "ControllerBase") { ControllerBase api = (ControllerBase)info.Assembly.CreateInstance(info.Type.FullName);; // 执行初始化调用 string res = api.Initialize(host); if (!res.IsNoneOrNull()) { return(httpContext.Response.WriteAsync(res)); } // 调用主事件 res = (string)info.Method.Invoke(api, null); dpz3.Modular.Result.Text text = new dpz3.Modular.Result.Text() { Content = res }; return(api.Render(text)); } if (info.ClassName == "SessionControllerBase") { SessionControllerBase api = (SessionControllerBase)info.Assembly.CreateInstance(info.Type.FullName);; // 执行初始化调用 string res = api.Initialize(host); Program.Println($"[*] 当前交互标识 {api.SessionID}"); using (var session = new it.Session(api.SessionID)) { // 自动申请新的交互标识 if (!session.Enable) { session.CreateNewSessionID(); api.SessionID = session.SessionID; Program.Println($"[+] 生成了一个全新的交互标识 {api.SessionID}"); } host.Session = session; if (!res.IsNoneOrNull()) { return(httpContext.Response.WriteAsync(res)); } // 调用主事件 res = (string)info.Method.Invoke(api, null); dpz3.Modular.Result.Text text = new dpz3.Modular.Result.Text() { Content = res }; return(api.Render(text)); } } if (info.ClassName == "JttpSessionControllerBase") { JttpSessionControllerBase api = (JttpSessionControllerBase)info.Assembly.CreateInstance(info.Type.FullName);; // 执行初始化调用 string res = api.Initialize(host); using (var session = new it.Session(api.SessionID)) { host.Session = session; if (!res.IsNoneOrNull()) { return(httpContext.Response.WriteAsync(res)); } // 调用主事件 res = (string)info.Method.Invoke(api, null); dpz3.Modular.Result.Text text = new dpz3.Modular.Result.Text() { Content = res }; return(api.Render(text)); } } return(httpContext.Response.WriteAsync($"尚未支持的基础类型 {info.ClassName}")); case "dpz3.Modular.IResult": // 处理返回为标准接口的函数定义 if (info.ClassName == "ControllerBase") { ControllerBase api = (ControllerBase)info.Assembly.CreateInstance(info.Type.FullName);; // 执行初始化调用 string res = api.Initialize(host); if (!res.IsNoneOrNull()) { return(httpContext.Response.WriteAsync(res)); } // 调用主事件 IResult result = (IResult)info.Method.Invoke(api, null); return(api.Render(result)); } if (info.ClassName == "SessionControllerBase") { SessionControllerBase api = (SessionControllerBase)info.Assembly.CreateInstance(info.Type.FullName);; // 执行初始化调用 string res = api.Initialize(host); Program.Println($"[*] 当前交互标识 {api.SessionID}"); using (var session = new it.Session(api.SessionID)) { // 自动申请新的交互标识 if (!session.Enable) { session.CreateNewSessionID(); api.SessionID = session.SessionID; Program.Println($"[+] 生成了一个全新的交互标识 {api.SessionID}"); } host.Session = session; if (!res.IsNoneOrNull()) { return(httpContext.Response.WriteAsync(res)); } // 调用主事件 IResult result = (IResult)info.Method.Invoke(api, null); return(api.Render(result)); } } if (info.ClassName == "JttpSessionControllerBase") { JttpSessionControllerBase api = (JttpSessionControllerBase)info.Assembly.CreateInstance(info.Type.FullName);; // 执行初始化调用 string res = api.Initialize(host); using (var session = new it.Session(api.SessionID)) { host.Session = session; if (!res.IsNoneOrNull()) { return(httpContext.Response.WriteAsync(res)); } // 调用主事件 IResult result = (IResult)info.Method.Invoke(api, null); return(api.Render(result)); } } return(httpContext.Response.WriteAsync($"尚未支持的基础类型 {info.ClassName}")); default: return(httpContext.Response.WriteAsync($"尚未支持的返回类型 {returnType}")); } } } catch (Exception ex) { return(httpContext.Response.WriteAsync(ex.ToString())); } }