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.

126 lines
3.6 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
3 years ago
4 years ago
3 years ago
4 years ago
  1. #if NETCORE
  2. using System;
  3. using System.Collections.Generic;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.Extensions.Hosting;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.AspNetCore.Builder;
  10. using System.Threading.Tasks;
  11. using System.Net;
  12. using System.Net.WebSockets;
  13. using System.Security.Cryptography.X509Certificates;
  14. using System.Net.Security;
  15. using System.Security.Authentication;
  16. namespace Apewer.Web
  17. {
  18. /// <summary>ASP.NET Core 服务器。</summary>
  19. public abstract class AspNetCoreServer : IDisposable
  20. {
  21. #region 通用。
  22. IHost _host;
  23. IHostBuilder _builder;
  24. private bool _running = false;
  25. /// <summary>正在运行。</summary>
  26. public bool Running { get => _running; }
  27. /// <summary>启动 Host。</summary>
  28. protected Exception Run<TStartup>(bool async) where TStartup : AspNetCoreStartup
  29. {
  30. try
  31. {
  32. _builder = Host.CreateDefaultBuilder();
  33. _builder.ConfigureWebHostDefaults((builder) =>
  34. {
  35. Configure(builder);
  36. builder.UseIIS();
  37. builder.UseStartup<TStartup>();
  38. });
  39. // 运行。
  40. _host = _builder.Build();
  41. if (async) _host.RunAsync();
  42. else _host.Run();
  43. return null;
  44. }
  45. catch (Exception ex)
  46. {
  47. return ex;
  48. }
  49. }
  50. /// <summary>配置 WebHost 构建程序。</summary>
  51. protected abstract void Configure(IWebHostBuilder builder);
  52. /// <summary>启动。</summary>
  53. public abstract void Run<T>() where T : AspNetCoreStartup;
  54. /// <summary>释放非托管资源。</summary>
  55. public void Dispose()
  56. {
  57. RuntimeUtility.Dispose(_host);
  58. _host = null;
  59. _running = false;
  60. }
  61. #endregion
  62. #region 用于派生类的属性。
  63. private int _port = -1;
  64. private bool _compression = false;
  65. private long _maxbody = 1073741824L;
  66. private X509Certificate2 _certificate = null;
  67. private SslProtocols _ssl = SslProtocols.Tls;
  68. /// <summary>监听的端口。默认值:自动选择 80 端口或 443 端口。</summary>
  69. public int Port
  70. {
  71. get { return (_port < 0) ? (_certificate == null ? 80 : 443) : _port; }
  72. set { if (!_running) _port = value.Restrict(0, 65535); }
  73. }
  74. /// <summary>使用的证书。默认值:无。</summary>
  75. public X509Certificate2 Certificate
  76. {
  77. get { return _certificate; }
  78. set { if (!_running) _certificate = value; }
  79. }
  80. /// <summary>请求的最大 Body 大小,单位为字节,最小为 1 MB。默认值:1073741824(1 GB)。</summary>
  81. public long MaxBody
  82. {
  83. get { return _maxbody; }
  84. set { if (!_running) _maxbody = value.Restrict(1048576L, 1073741824L); }
  85. }
  86. /// <summary>使用 SSL 时的协议。默认值:TLS 1.0。</summary>
  87. public SslProtocols SslProtocol
  88. {
  89. get { return _ssl; }
  90. set { if (!_running) _ssl = value; }
  91. }
  92. /// <summary>尝试启用压缩。默认值:不启用。</summary>
  93. public bool Compression
  94. {
  95. get { return _compression; }
  96. set { if (!_running) _compression = value; }
  97. }
  98. #endregion
  99. }
  100. }
  101. #endif