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.

59 lines
1.7 KiB

4 years ago
  1. #if NETCORE
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Server.Kestrel.Core;
  4. using System.Net;
  5. namespace Apewer.Web
  6. {
  7. /// <summary>Kestrel 服务器。</summary>
  8. public class KestrelServer<TStartup> : KestrelServer where TStartup : AspNetCoreStartup
  9. {
  10. /// <summary>启动。</summary>
  11. public virtual void Run() => Run<TStartup>(false);
  12. }
  13. /// <summary>Kestrel 服务器。</summary>
  14. public class KestrelServer : AspNetCoreServer
  15. {
  16. /// <summary>配置 WebHost 构建程序。</summary>
  17. protected override void Configure(IWebHostBuilder builder) => builder.ConfigureKestrel(Configure);
  18. /// <summary>配置 Kestrel 服务器。</summary>
  19. protected virtual void Configure(KestrelServerOptions options)
  20. {
  21. // 同步 IO。
  22. options.AllowSynchronousIO = true;
  23. // 限制请求大小。
  24. options.Limits.MaxRequestBodySize = MaxBody;
  25. // 在 Response 中不包含 Server 属性。
  26. options.AddServerHeader = false;
  27. // 配置端口和证书。
  28. var certificate = Certificate;
  29. if (certificate == null) options.Listen(IPAddress.Any, Port);
  30. else options.Listen(IPAddress.Any, Port, (listen) =>
  31. {
  32. listen.UseHttps(certificate, (ao) =>
  33. {
  34. ao.CheckCertificateRevocation = false;
  35. ao.ServerCertificate = certificate;
  36. ao.AllowAnyClientCertificate();
  37. });
  38. });
  39. }
  40. /// <summary>启动。</summary>
  41. public override void Run<T>() => Run<T>(false);
  42. }
  43. }
  44. #endif