diff --git a/Apewer/UnparallelAttribute.cs b/Apewer/UnparallelAttribute.cs new file mode 100644 index 0000000..d5b39a2 --- /dev/null +++ b/Apewer/UnparallelAttribute.cs @@ -0,0 +1,16 @@ +using System; + +namespace Apewer +{ + + /// 表示方法不可并行。 + [AttributeUsage(AttributeTargets.Method)] + public sealed class UnparallelAttribute : Attribute + { + + /// + public static implicit operator bool(UnparallelAttribute unparallel) => unparallel != null; + + } + +} diff --git a/Apewer/Web/ApiAction.cs b/Apewer/Web/ApiAction.cs index 26db784..8f88482 100644 --- a/Apewer/Web/ApiAction.cs +++ b/Apewer/Web/ApiAction.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Reflection; -using System.Text; namespace Apewer.Web { @@ -19,6 +18,7 @@ namespace Apewer.Web string _path = null; HttpMethod[] _methods = null; ApiParameter[] _parameters = null; + UnparallelAttribute _unparallel = null; #endregion @@ -55,6 +55,9 @@ namespace Apewer.Web } } + /// 不可并行。 + public UnparallelAttribute Unparallel { get => _unparallel; } + /// 生成 JSON 实例。 public Json ToJson() => ToJson(null); @@ -109,9 +112,10 @@ namespace Apewer.Web /// URL 路径。 /// HTTP 方法。 /// 参数。 + /// 不可并行。 /// /// - ApiAction(Type type, MethodInfo method, string path, HttpMethod[] methods, ApiParameter[] parameters) + ApiAction(Type type, MethodInfo method, string path, HttpMethod[] methods, ApiParameter[] parameters, UnparallelAttribute unparallel) { if (type == null) throw new ArgumentNullException(nameof(type)); if (method == null) throw new ArgumentNullException(nameof(method)); @@ -128,6 +132,7 @@ namespace Apewer.Web _path = path; _methods = methods; _parameters = parameters; + _unparallel = unparallel; } /// 解析控制器类型,获取 API 活动。 @@ -213,7 +218,10 @@ namespace Apewer.Web parameters.Add(parameter); } - var action = new ApiAction(type, method, path, httpMethods.ToArray(), parameters.ToArray()); + // 不可并行 + var unparallel = RuntimeUtility.GetAttribute(method); + + var action = new ApiAction(type, method, path, httpMethods.ToArray(), parameters.ToArray(), unparallel); actions.Add(action); } diff --git a/Apewer/Web/ApiProcessor.cs b/Apewer/Web/ApiProcessor.cs index cf0d26f..dec3352 100644 --- a/Apewer/Web/ApiProcessor.cs +++ b/Apewer/Web/ApiProcessor.cs @@ -452,10 +452,7 @@ namespace Apewer.Web if (initializer != null) { var @continue = true; - UnparallelLocker.InLock(GetUnparallelKey(controller.GetType()), () => - { - @continue = initializer.Invoke(controller); - }); + @continue = initializer.Invoke(controller); if (!@continue) return; } @@ -466,10 +463,7 @@ namespace Apewer.Web { // 调用 API,获取返回值。 _context.Controller = controller; - UnparallelLocker.InLock(GetUnparallelKey(function.Method), () => - { - Invoke(_context, function.Method, function.Parameters); - }); + Invoke(_context, function.Method, function.Parameters); } else { @@ -477,11 +471,8 @@ namespace Apewer.Web var @default = ApiUtility.GetDefault(controller); if (@default != null) { - UnparallelLocker.InLock(GetUnparallelKey(controller.GetType()), () => - { - @default.Invoke(controller); - return; - }); + @default.Invoke(controller); + return; } // 没有执行任何 Function,尝试枚举。 diff --git a/Apewer/Web/ApiUtility.cs b/Apewer/Web/ApiUtility.cs index 79356d8..2c01b9c 100644 --- a/Apewer/Web/ApiUtility.cs +++ b/Apewer/Web/ApiUtility.cs @@ -736,6 +736,14 @@ namespace Apewer.Web { try { + if (exception is IApiException apiException) + { + if (!string.IsNullOrEmpty(apiException.Status)) + { + response.Status = apiException.Status.ToLower(); + } + } + if (setData) { var json = ToJson(exception);