diff --git a/Apewer/UnparallelAttribute.cs b/Apewer/UnparallelAttribute.cs index d5b39a2..5b2e89f 100644 --- a/Apewer/UnparallelAttribute.cs +++ b/Apewer/UnparallelAttribute.cs @@ -4,7 +4,7 @@ namespace Apewer { /// 表示方法不可并行。 - [AttributeUsage(AttributeTargets.Method)] + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] public sealed class UnparallelAttribute : Attribute { diff --git a/Apewer/Web/ApiApplication.cs b/Apewer/Web/ApiApplication.cs index 6d5b348..7850eb3 100644 --- a/Apewer/Web/ApiApplication.cs +++ b/Apewer/Web/ApiApplication.cs @@ -22,6 +22,7 @@ namespace Apewer.Web // invoke & enumerate bool _independent = false; bool _hidden = false; + bool _unparallel = false; // functions Dictionary _dict = new Dictionary(); @@ -52,6 +53,9 @@ namespace Apewer.Web /// public bool Hidden { get => _hidden; } + /// + public bool Unparallel { get => _unparallel; } + /// public ApiFunction[] Functions { get => _list.ToArray(); } @@ -98,6 +102,9 @@ namespace Apewer.Web // independent if (type.Contains(false)) _independent = true; + // unparallel + if (type.Contains(false)) _unparallel = true; + // Module var assemblyName = type.Assembly.GetName(); _module = TextUtility.Join("-", assemblyName.Name, assemblyName.Version.ToString()); diff --git a/Apewer/Web/ApiFunction.cs b/Apewer/Web/ApiFunction.cs index 885ca94..b615bf0 100644 --- a/Apewer/Web/ApiFunction.cs +++ b/Apewer/Web/ApiFunction.cs @@ -22,6 +22,7 @@ namespace Apewer.Web string _description = null; bool _hidden = false; + bool _unparallel = false; #endregion @@ -51,6 +52,9 @@ namespace Apewer.Web /// public bool Hidden { get => _hidden; } + /// + public bool Unparallel { get => _unparallel; } + #endregion /// @@ -118,6 +122,9 @@ namespace Apewer.Web // hidden if (method.Contains(false)) _hidden = true; + // unparallel + if (method.Contains(false)) _unparallel = true; + // 参数。 _parameters = parameters; } diff --git a/Apewer/Web/ApiProcessor.cs b/Apewer/Web/ApiProcessor.cs index dec3352..d174368 100644 --- a/Apewer/Web/ApiProcessor.cs +++ b/Apewer/Web/ApiProcessor.cs @@ -404,12 +404,25 @@ namespace Apewer.Web } else { + // 创建默认控制器。 var controller = null as ApiController; try { - controller = CreateController(@default, _context); - InvokeFunction(controller, application, null, options, request, response); + var unparallel = @default.Contains(false); + if (unparallel) + { + UnparallelLocker.InLock(GetUnparallelKey(@default), () => + { + controller = CreateController(@default, _context); + InvokeFunction(controller, application, null, options, request, response); + }); + } + else + { + controller = CreateController(@default, _context); + InvokeFunction(controller, application, null, options, request, response); + } } catch (Exception ex) { @@ -428,8 +441,27 @@ namespace Apewer.Web var controller = null as ApiController; try { - controller = CreateController(application.Type, _context); - InvokeFunction(controller, application, function, options, request, response); + if (application.Unparallel) + { + UnparallelLocker.InLock(GetUnparallelKey(application.Type), () => + { + controller = CreateController(application.Type, _context); + InvokeFunction(controller, application, function, options, request, response); + }); + } + else if (function != null && function.Unparallel) + { + UnparallelLocker.InLock(GetUnparallelKey(function.Method), () => + { + controller = CreateController(application.Type, _context); + InvokeFunction(controller, application, function, options, request, response); + }); + } + else + { + controller = CreateController(application.Type, _context); + InvokeFunction(controller, application, function, options, request, response); + } } catch (Exception ex) { @@ -831,7 +863,7 @@ namespace Apewer.Web static string GetUnparallelKey(Type type) { - return $"{type.DeclaringType.Assembly.ToString()}"; + return $"{type.Assembly.ToString()} | {type.FullName}"; } #endregion