Browse Source

API 反射支持 Unparallel 特性。

master
Elivo 1 month ago
parent
commit
c1907bff9c
  1. 2
      Apewer/UnparallelAttribute.cs
  2. 7
      Apewer/Web/ApiApplication.cs
  3. 7
      Apewer/Web/ApiFunction.cs
  4. 42
      Apewer/Web/ApiProcessor.cs

2
Apewer/UnparallelAttribute.cs

@ -4,7 +4,7 @@ namespace Apewer
{ {
/// <summary>表示方法不可并行。</summary> /// <summary>表示方法不可并行。</summary>
[AttributeUsage(AttributeTargets.Method)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class UnparallelAttribute : Attribute public sealed class UnparallelAttribute : Attribute
{ {

7
Apewer/Web/ApiApplication.cs

@ -22,6 +22,7 @@ namespace Apewer.Web
// invoke & enumerate // invoke & enumerate
bool _independent = false; bool _independent = false;
bool _hidden = false; bool _hidden = false;
bool _unparallel = false;
// functions // functions
Dictionary<string, ApiFunction> _dict = new Dictionary<string, ApiFunction>(); Dictionary<string, ApiFunction> _dict = new Dictionary<string, ApiFunction>();
@ -52,6 +53,9 @@ namespace Apewer.Web
/// <summary></summary> /// <summary></summary>
public bool Hidden { get => _hidden; } public bool Hidden { get => _hidden; }
/// <summary></summary>
public bool Unparallel { get => _unparallel; }
/// <summary></summary> /// <summary></summary>
public ApiFunction[] Functions { get => _list.ToArray(); } public ApiFunction[] Functions { get => _list.ToArray(); }
@ -98,6 +102,9 @@ namespace Apewer.Web
// independent // independent
if (type.Contains<IndependentAttribute>(false)) _independent = true; if (type.Contains<IndependentAttribute>(false)) _independent = true;
// unparallel
if (type.Contains<UnparallelAttribute>(false)) _unparallel = true;
// Module // Module
var assemblyName = type.Assembly.GetName(); var assemblyName = type.Assembly.GetName();
_module = TextUtility.Join("-", assemblyName.Name, assemblyName.Version.ToString()); _module = TextUtility.Join("-", assemblyName.Name, assemblyName.Version.ToString());

7
Apewer/Web/ApiFunction.cs

@ -22,6 +22,7 @@ namespace Apewer.Web
string _description = null; string _description = null;
bool _hidden = false; bool _hidden = false;
bool _unparallel = false;
#endregion #endregion
@ -51,6 +52,9 @@ namespace Apewer.Web
/// <summary></summary> /// <summary></summary>
public bool Hidden { get => _hidden; } public bool Hidden { get => _hidden; }
/// <summary></summary>
public bool Unparallel { get => _unparallel; }
#endregion #endregion
/// <summary></summary> /// <summary></summary>
@ -118,6 +122,9 @@ namespace Apewer.Web
// hidden // hidden
if (method.Contains<HiddenAttribute>(false)) _hidden = true; if (method.Contains<HiddenAttribute>(false)) _hidden = true;
// unparallel
if (method.Contains<UnparallelAttribute>(false)) _unparallel = true;
// 参数。 // 参数。
_parameters = parameters; _parameters = parameters;
} }

42
Apewer/Web/ApiProcessor.cs

@ -404,12 +404,25 @@ namespace Apewer.Web
} }
else else
{ {
// 创建默认控制器。 // 创建默认控制器。
var controller = null as ApiController; var controller = null as ApiController;
try try
{ {
controller = CreateController(@default, _context);
InvokeFunction(controller, application, null, options, request, response);
var unparallel = @default.Contains<UnparallelAttribute>(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) catch (Exception ex)
{ {
@ -428,8 +441,27 @@ namespace Apewer.Web
var controller = null as ApiController; var controller = null as ApiController;
try 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) catch (Exception ex)
{ {
@ -831,7 +863,7 @@ namespace Apewer.Web
static string GetUnparallelKey(Type type) static string GetUnparallelKey(Type type)
{ {
return $"{type.DeclaringType.Assembly.ToString()}";
return $"{type.Assembly.ToString()} | {type.FullName}";
} }
#endregion #endregion

Loading…
Cancel
Save