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.

116 lines
3.3 KiB

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().ConfigureWebHostDefaults((builder) =>
  33. {
  34. Configure(builder);
  35. builder.UseStartup<TStartup>();
  36. });
  37. // 运行。
  38. _host = _builder.Build();
  39. if (async) _host.RunAsync();
  40. else _host.Run();
  41. return null;
  42. }
  43. catch (Exception ex)
  44. {
  45. return ex;
  46. }
  47. }
  48. /// <summary>配置 WebHost 构建程序。</summary>
  49. protected abstract void Configure(IWebHostBuilder builder);
  50. /// <summary>启动。</summary>
  51. public abstract void Run<T>() where T : AspNetCoreStartup;
  52. /// <summary>释放非托管资源。</summary>
  53. public void Dispose()
  54. {
  55. RuntimeUtility.Dispose(_host);
  56. _host = null;
  57. _running = false;
  58. }
  59. #endregion
  60. #region 用于派生类的属性。
  61. private int _port = -1;
  62. private long _maxbody = 1073741824L;
  63. private X509Certificate2 _certificate = null;
  64. private SslProtocols _ssl = SslProtocols.Tls;
  65. /// <summary>监听的端口。默认值:自动选择 80 端口或 443 端口。</summary>
  66. public int Port
  67. {
  68. get { return (_port < 0) ? (_certificate == null ? 80 : 443) : _port; }
  69. set { if (!_running) _port = value.Restrict(0, 65535); }
  70. }
  71. /// <summary>使用的证书。默认值:无。</summary>
  72. public X509Certificate2 Certificate
  73. {
  74. get { return _certificate; }
  75. set { if (!_running) _certificate = value; }
  76. }
  77. /// <summary>请求的最大 Body 大小,单位为字节,最小为 1 MB。默认值:1073741824(1 GB)。</summary>
  78. public virtual long MaxBody
  79. {
  80. get { return _maxbody; }
  81. set { if (!_running) _maxbody = value.Restrict(1048576L, 1073741824L); }
  82. }
  83. /// <summary>使用 SSL 时的协议。默认值:TLS 1.0。</summary>
  84. public virtual SslProtocols SslProtocol
  85. {
  86. get { return _ssl; }
  87. set { if (!_running) _ssl = value; }
  88. }
  89. #endregion
  90. }
  91. }
  92. #endif