You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.3 KiB

4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
2 years ago
4 years ago
3 years ago
4 years ago
11 months ago
4 years ago
11 months ago
4 years ago
11 months ago
4 years ago
11 months ago
4 years ago
11 months ago
4 years ago
2 years ago
11 months ago
4 years ago
  1. using Apewer.Network;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Reflection;
  6. namespace Apewer.Web
  7. {
  8. /// <summary>API 调用器。</summary>
  9. public sealed class ApiInvoker
  10. {
  11. #region
  12. private static ApiInvoker _default = new ApiInvoker();
  13. /// <summary>默认调用器实例。</summary>
  14. public static ApiInvoker Default { get => _default; }
  15. #endregion
  16. object _locker = new object();
  17. bool _initialized = false;
  18. /// <summary>获取或设置要使用的 API 入口。</summary>
  19. public ApiEntries Entries { get; set; }
  20. /// <summary>获取或设置 Logger。</summary>
  21. public Logger Logger { get; set; }
  22. /// <summary>获取或设置选项。</summary>
  23. public ApiOptions Options { get; set; }
  24. /// <summary>异常捕获程序。</summary>
  25. public Action<ApiCatch> Catcher { get; set; }
  26. /// <summary>输出前的检查。</summary>
  27. public ApiPreOutput PreOutput { get; set; }
  28. /// <summary>执行初始化程序,每个 ApiInvoker 实例仅执行一次初始化。</summary>
  29. public void Initialize(Action action)
  30. {
  31. if (action == null) return;
  32. lock (_locker)
  33. {
  34. if (_initialized) return;
  35. action.Invoke();
  36. _initialized = true;
  37. }
  38. }
  39. /// <summary>发起调用。</summary>
  40. /// <exception cref="ArgumentNullException" />
  41. public void Invoke(ApiProvider provider)
  42. {
  43. if (provider == null) throw new ArgumentNullException(nameof(provider));
  44. var entries = Entries;
  45. if (entries == null) entries = ApiEntries.AppDomain();
  46. Entries = entries;
  47. Invoke(provider, entries);
  48. }
  49. /// <summary>发起调用。</summary>
  50. /// <exception cref="ArgumentNullException" />
  51. public void Invoke(ApiProvider provider, ApiEntries entries)
  52. {
  53. if (provider == null) throw new ArgumentNullException(nameof(provider));
  54. if (entries == null) throw new ArgumentNullException(nameof(entries));
  55. var context = new ApiContext(this, provider, entries);
  56. var processor = new ApiProcessor(context);
  57. processor.Run();
  58. }
  59. }
  60. }