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.

65 lines
2.6 KiB

4 years ago
  1. #if NETCORE
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.AspNetCore.Server.HttpSys;
  4. namespace Apewer.Web
  5. {
  6. /// <summary>HTTP.sys 服务器。</summary>
  7. /// <remarks>
  8. /// <para>HTTP.sys 是仅在 Windows 上运行的适用于 ASP.NET Core 的 Web 服务器,提供了一些 Kestrel 不提供的功能。</para>
  9. /// <para>HTTP.sys 支持以下功能:<br />Windows 身份验证;<br />端口共享;<br />具有 SNI 的 HTTPS;<br />基于 TLS 的 HTTP/2(Windows 10 或更高版本);<br />直接文件传输;<br />响应缓存;<br />WebSocket(Windows 8 或更高版本)。</para>
  10. /// </remarks>
  11. public class HttpSysServer<TStartup> : HttpSysServer where TStartup : AspNetCoreStartup
  12. {
  13. /// <summary>启动。</summary>
  14. public virtual void Run() => Run<TStartup>(false);
  15. }
  16. /// <summary>HTTP.sys 服务器。</summary>
  17. /// <remarks>
  18. /// <para>HTTP.sys 是仅在 Windows 上运行的适用于 ASP.NET Core 的 Web 服务器,提供了一些 Kestrel 不提供的功能。</para>
  19. /// <para>HTTP.sys 支持以下功能:<br />Windows 身份验证;<br />端口共享;<br />具有 SNI 的 HTTPS;<br />基于 TLS 的 HTTP/2(Windows 10 或更高版本);<br />直接文件传输;<br />响应缓存;<br />WebSocket(Windows 8 或更高版本)。</para>
  20. /// </remarks>
  21. public class HttpSysServer : AspNetCoreServer
  22. {
  23. /// <summary>配置 WebHost 构建程序。</summary>
  24. protected override void Configure(IWebHostBuilder builder) => builder.UseHttpSys(Configure);
  25. /// <summary>配置 HTTP.sys 服务器。</summary>
  26. protected virtual void Configure(HttpSysOptions options)
  27. {
  28. // 同步 IO。
  29. options.AllowSynchronousIO = true;
  30. // 限制请求大小。
  31. options.MaxRequestBodySize = MaxBody;
  32. // 禁用内核缓存。
  33. options.EnableResponseCaching = false;
  34. // 认证。
  35. options.Authentication.Schemes = Microsoft.AspNetCore.Server.HttpSys.AuthenticationSchemes.None;
  36. options.Authentication.AllowAnonymous = true;
  37. // 配置端口和证书。
  38. var ip = "0.0.0.0";
  39. var port = Port;
  40. var certificate = Certificate;
  41. var protocol = certificate == null ? "http" : "https";
  42. var prefix = $"{protocol}://{ip}:{port}";
  43. options.UrlPrefixes.Add(prefix);
  44. }
  45. /// <summary>启动。</summary>
  46. public override void Run<T>() => Run<T>(false);
  47. }
  48. }
  49. #endif