/// <summary> /// 编译控制器 /// </summary> /// <param name="type"></param> /// <param name="routed"></param> protected void ComplieController(Type type, Routed baseRouted) { //类的属性标记中的基础路径 MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); List <IRoute> routeList = new List <IRoute>(); IRoute route; foreach (MethodInfo method in methods) { if ((route = ComplieFunction(type, method, baseRouted)) != null) { routeList.Add(route); } } if (baseRouted == null) { return; } Dictionary <string, string> controllerWhere = ComplieDirection(baseRouted.Where); Dictionary <string, string> controllerDefaults = ComplieDirection(baseRouted.Defaults); for (int i = 0; i < routeList.Count; i++) { route = routeList[i]; ComplieOptionsGroup(route, baseRouted); ComplieOptionsWhere(route, controllerWhere); ComplieOptionsDefaults(route, controllerDefaults); } }
/// <summary> /// 增加组信息 /// </summary> /// <param name="route"></param> /// <param name="routed"></param> protected void ComplieOptionsGroup(IRoute route, Routed routed) { if (!string.IsNullOrEmpty(routed.Group)) { route.Group(routed.Group); } }
public bool CanRoute(Routed route, object message) { Uri uri; return(route != null && Uri.TryCreate(route.Route, UriKind.Absolute, out uri) && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)); }
/// <summary> /// 编译属性路由 /// </summary> /// <param name="type"></param> protected void ComplieRouted(Type type) { if (!type.IsDefined(this.routed, false)) { return; } object[] obj = type.GetCustomAttributes(this.routed, false); if (obj.Length <= 0) { return; } controllerFuncBuildRecord.Clear(); Routed routed = null; for (int i = 0; i < obj.Length; i++) { routed = obj[i] as Routed; if (routed == null) { continue; } if (string.IsNullOrEmpty(routed.Path)) { routed.Path = router.GetDefaultScheme() + "://" + ClassOrFunctionNameToRouteName(type.Name); } ComplieController(type, routed); } }
/// <summary> /// 编译函数 /// </summary> /// <param name="method"></param> /// <param name="baseRouted"></param> protected IRoute[] ComplieFunction(Type type, MethodInfo method, Routed baseRouted) { object[] routeds = method.GetCustomAttributes(this.routed, false); if (routeds.Length <= 0) { return(null); } List <IRoute> ret = new List <IRoute>(); Routed routed = null; for (int i = 0; i < routeds.Length; i++) { routed = routeds[i] as Routed; //如果没有给定方法路由名则默认提供 if (string.IsNullOrEmpty(routed.Path)) { routed.Path = ClassOrFunctionNameToRouteName(method.Name); } //如果是包含scheme完整的uri那么则忽略来自控制器提供的uri //这里的所有的开头都不允许出现‘/’ string path = routed.Path.TrimStart('/'); if (baseRouted != null && !HasScheme(routed.Path)) { //如果开发者提供了控制器的路由配置,那么将会合并控制器路由的全局部分 int index = baseRouted.Path.LastIndexOf("://"); if (index != -1 && (index + 3) == baseRouted.Path.Length) { path = baseRouted.Path + path; } else { path = baseRouted.Path.TrimEnd('/') + "/" + path; } } // 检查控制器内是否重复编译 if (controllerFuncBuildRecord.ContainsKey(path + "$" + method.Name)) { continue; } controllerFuncBuildRecord.Add(path + "$" + method.Name, true); // 检查全局是否重复编译 CheckRepeat(path, type, method); IRoute route = router.Reg(path, type, method.Name); //编译标记中的属性路由中的配置到路由条目中 ComplieOptions(route, routed); ret.Add(route); } return(ret.ToArray()); }
private static string GetResourceUri(Routed route, object message) { if (!string.IsNullOrEmpty(route.Tag)) { return($"tag/{GetApplicationName()}/{route.Tag}"); } var prefix = message is IAsyncNotification ? "publish" : "process"; var path = GetRequestPath(message); return(path != null ? $"{prefix}/{path}" : prefix); }
public async Task <object> Route(Routed route, object message, IMediator mediator) { var response = await mediator .PostAsync <Message, HttpResponseMessage>( new Message(message), post => { post.BaseAddress = route.Route; post.ResourceUri = GetResourceUri(route, message); post.TypeNameHandling = true; }); if (response.IsSuccessStatusCode) { var payload = await response.Content .ReadAsAsync <Message>(RestFormatters.TypedJsonList); return(payload.Payload); } if (response.StatusCode == HttpStatusCodeExtensions.UnprocessableEntity && response.ReasonPhrase == "Validation") { var errors = (await response.Content .ReadAsAsync <ValidationFailureShim[]>(RestFormatters.JsonList)) .Select(e => (ValidationFailure)e); throw new ValidationException(errors); } if (response.ReasonPhrase == "HttpError") { var error = await response.Content.ReadAsAsync <HttpError>(); LogExceptionDetails(error); object exceptionType; if (error.TryGetValue("ExceptionType", out exceptionType) && Equals(exceptionType, typeof(InvalidOperationException).FullName)) { throw new InvalidOperationException(error.ExceptionMessage ?? error.Message); } throw new Exception(error.ExceptionMessage ?? error.Message); } response.EnsureSuccessStatusCode(); return(null); }
/// <summary> /// 编译属性路由 /// </summary> /// <param name="type"></param> protected void ComplieRouted(Type type) { if (!(target.IsAssignableFrom(type) && type != target)) { return; } object[] obj = type.GetCustomAttributes(this.routed, true); Routed routed = null; if (obj.Length > 0) { routed = obj[0] as Routed; } ComplieController(type, routed); }
/// <summary> /// 编译函数 /// </summary> /// <param name="method"></param> /// <param name="baseRouted"></param> protected IRoute ComplieFunction(Type type, MethodInfo method, Routed baseRouted) { object[] routeds = method.GetCustomAttributes(this.routed, false); if (routeds.Length <= 0) { return(null); } Routed routed = routeds[0] as Routed; if (routed == null) { return(null); } //如果是包含scheme完整的uri那么则忽略来自控制器提供的uri //这里的所有的开头都不允许出现‘/’ string path = routed.Path.TrimStart('/'); if (baseRouted != null && !HasScheme(routed.Path)) { //如果开发者提供了控制器的路由配置,那么将会合并控制器路由的全局部分 int index = baseRouted.Path.LastIndexOf("://"); if (index != -1 && (index + 3) == baseRouted.Path.Length) { path = baseRouted.Path + path; } else { path = baseRouted.Path.TrimEnd('/') + "/" + path; } } IRoute route = router.Reg(path, type, method.Name); //编译标记中的属性路由中的配置到路由条目中 ComplieOptions(route, routed); return(route); }
/// <summary> /// 编译配置信息 /// </summary> /// <param name="route"></param> /// <param name="routed"></param> protected void ComplieOptions(IRoute route, Routed routed) { ComplieOptionsGroup(route, routed); ComplieOptionsWhere(route, ComplieDirection(routed.Where)); ComplieOptionsDefaults(route, ComplieDirection(routed.Defaults)); }
public Task <object> Route(Routed route, object message, IMediator mediator) { return(Task.FromResult(null as object)); }
public bool CanRoute(Routed route, object message) { return(route.Route == "Trash"); }