/// <summary> /// 结束通知 /// </summary> /// <param name="context"></param> /// <param name="success"></param> private void SagaSubError(BaseModule context) { context.Input.TryGetValue(GlobalTraceId, out string globalId); Dictionary <string, string> response = new Dictionary <string, string>(); response.Add("globalTraceId", globalId); context.InvokeProcessor("Anno.Plugs.DTransaction", "DTransaction", "SagaSubError", response); }
/// <summary> /// 结束通知 /// </summary> /// <param name="context"></param> /// <param name="success"></param> private void SagaEnd(BaseModule context, bool success) { context.Input.TryGetValue(GlobalTraceId, out string globalId); Dictionary <string, string> response = new Dictionary <string, string>(); response.Add("globalTraceId", globalId); response.Add("sagaStartSuccess", success.ToString()); context.InvokeProcessor("Anno.Plugs.DTransaction", "DTransaction", "SagaEnd", response); }
/* * 事务开始前 */ public void OnActionExecuting(BaseModule context) { context.Input.TryGetValue(GlobalTraceId, out string globalId); Dictionary <string, string> response = new Dictionary <string, string>(); response.Add("globalTraceId", globalId); response.Add("executionTimeSpan", (ExecutionSeconds * 1000).ToString()); response.Add("recover", this.RecoverMethod); response.Add("sagaInput", JsonConvert.SerializeObject(context.Input)); context.InvokeProcessor("Anno.Plugs.DTransaction", "DTransaction", "SagaStarted", response); }
/* * 业务执行后 */ public void OnActionExecuted(BaseModule context) { if (context.ActionResult.Status) { context.Input.TryGetValue(GlobalTraceId, out string globalId); context.Input.TryGetValue(TraceId, out string traceId); Dictionary <string, string> response = new Dictionary <string, string>(); response.Add("globalTraceId", globalId); response.Add("traceId", traceId); response.Add("recover", this.RecoverMethod); response.Add("sagaInput", JsonConvert.SerializeObject(context.Input)); response.Add("sagaRlt", JsonConvert.SerializeObject(context.ActionResult)); context.InvokeProcessor("Anno.Plugs.DTransaction", "DTransaction", "SagaSub", response); } else { SagaSubError(context); } }
/* * 业务异常后(冲正) */ public void OnException(Exception ex, BaseModule context) { SagaSubError(context); }
/* * 事务开始前 */ public void OnActionExecuting(BaseModule context) { }
/// <summary> /// 根据服务转发 /// </summary> /// <param name="input"></param> /// <param name="type">表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型</param> /// <returns></returns> public static ActionResult Transmit(Dictionary <string, string> input, Routing.RoutInfo routInfo) { BaseModule module = null; try { Interlocked.Increment(ref engineCounter); #region Cache string key = string.Empty; if (routInfo.CacheMiddleware.Count > 0) { key = GetDicStrHashCode(input); if (TryGetCache(routInfo, key, out ActionResult rltCache)) { return(rltCache); } } #endregion List <object> lo = new List <object>() { input }; module = Loader.IocLoader.Resolve <BaseModule>(routInfo.RoutModuleType); var init = module.Init(input); if (!init) { return(new ActionResult() { Status = false, Msg = "Init拦截!" }); } if (routInfo.RoutMethod == null) { return(new ActionResult() { Status = false, OutputData = null, Msg = $"在【{input[Eng.NAMESPACE]}】中找不到【{input[Eng.CLASS]}.{input[Eng.METHOD]}】!" }); } #region Authorization for (int i = 0; i < routInfo.AuthorizationFilters.Count; i++) { routInfo.AuthorizationFilters[i].OnAuthorization(module); if (!module.Authorized) { return(module.ActionResult == null ? new ActionResult() { Status = false, OutputData = 401, Msg = "401,Unauthrized" } : module.ActionResult ); } } #endregion for (int i = 0; i < routInfo.ActionFilters.Count; i++) { routInfo.ActionFilters[i].OnActionExecuting(module); } #region 调用业务方法 object rltCustomize = null; if (routInfo.ReturnTypeIsTask) { var rlt = (routInfo.RoutMethod.Invoke(module, DicToParameters(routInfo.RoutMethod, input).ToArray()) as Task); rltCustomize = routInfo.RoutMethod.ReturnType.GetProperty("Result").GetValue(rlt, null); } else { rltCustomize = routInfo.RoutMethod.Invoke(module, DicToParameters(routInfo.RoutMethod, input).ToArray()); } if (routInfo.ReturnTypeIsIActionResult && rltCustomize != null) { module.ActionResult = rltCustomize as ActionResult; } else { module.ActionResult = new ActionResult(true, rltCustomize); } #endregion for (int i = (routInfo.ActionFilters.Count - 1); i >= 0; i--) { routInfo.ActionFilters[i].OnActionExecuted(module); } if (routInfo.CacheMiddleware.Count > 0) { AddCache(routInfo, key, module.ActionResult); } return(module.ActionResult); } catch (Exception ex) { if (routInfo.RoutMethod != null) { if (module.ActionResult != null) { module.ActionResult.Status = false; module.ActionResult.Msg = ex.InnerException?.Message ?? ex.Message; } foreach (var ef in routInfo.ExceptionFilters) { ef.OnException(ex, module); } } #if DEBUG //记录日志 Log.Log.Error(ex, routInfo.RoutModuleType); #endif return(module.ActionResult ?? new ActionResult() { Status = false, OutputData = null, Msg = ex.InnerException?.Message ?? ex.Message }); } finally { Interlocked.Decrement(ref engineCounter); } }
/// <summary> /// 根据服务转发 /// </summary> /// <param name="input"></param> /// <param name="type">表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型</param> /// <returns></returns> public static ActionResult Transmit(Dictionary <string, string> input, Routing.RoutInfo routInfo) { BaseModule module = null; try { #region Cache string key = string.Empty; if (routInfo.CacheMiddleware.Count > 0) { key = GetDicStrHashCode(input); if (TryGetCache(routInfo, key, out ActionResult rltCache)) { return(rltCache); } } #endregion List <object> lo = new List <object>() { input }; module = Loader.IocLoader.Resolve <BaseModule>(routInfo.RoutModuleType); //创建实例(无参构造器) var init = module.Init(input); if (!init) { return(new ActionResult() { Status = false, Msg = "Init拦截!" }); } if (routInfo.RoutMethod == null) { return(new ActionResult() { Status = false, OutputData = null, Msg = $"在【{input[Eng.NAMESPACE]}】中找不到【{input[Eng.CLASS]}.{input[Eng.METHOD]}】!" }); } #region Authorization for (int i = 0; i < routInfo.AuthorizationFilters.Count; i++) { routInfo.AuthorizationFilters[i].OnAuthorization(module); if (!module.Authorized) { return(new ActionResult() { Status = false, OutputData = 401, Msg = "401,Unauthrized" }); } } #endregion for (int i = 0; i < routInfo.ActionFilters.Count; i++) { routInfo.ActionFilters[i].OnActionExecuting(module); } var rltCustomize = routInfo.RoutMethod.Invoke(module, DicToParameters(routInfo.RoutMethod, input).ToArray()); if (rltCustomize != null && typeof(IActionResult).IsAssignableFrom(rltCustomize.GetType())) { module.ActionResult = rltCustomize as ActionResult; } else { module.ActionResult = new ActionResult(true, rltCustomize); } for (int i = (routInfo.ActionFilters.Count - 1); i >= 0; i--) { routInfo.ActionFilters[i].OnActionExecuted(module); } if (routInfo.CacheMiddleware.Count > 0) { AddCache(routInfo, key, module.ActionResult); } return(module.ActionResult); } catch (Exception ex) { if (routInfo.RoutMethod != null) { foreach (var ef in routInfo.ExceptionFilters) { ef.OnException(ex, module); } } //记录日志 Log.Log.Error(ex, routInfo.RoutModuleType); return(new ActionResult() { Status = false, OutputData = null, Msg = ex.InnerException?.Message ?? ex.Message }); } }
/* * 业务异常后 */ public void OnException(Exception ex, BaseModule context) { SagaEnd(context, false); }
/* * 业务执行后 */ public void OnActionExecuted(BaseModule context) { SagaEnd(context, context.ActionResult.Status); }