Host.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.Extensions.FileProviders;
  6. namespace Vit.WebHost
  7. {
  8. public class Host
  9. {
  10. #region Run
  11. public static void Run(int port = 8888, string wwwrootPath = null, Action<IApplicationBuilder> OnConfigure = null)
  12. {
  13. Run(wwwrootPath, OnConfigure, "http://*:" + port);
  14. }
  15. public static void Run(string wwwrootPath = null, Action<IApplicationBuilder> OnConfigure = null, params string[] urls)
  16. {
  17. Run(new RunArg
  18. {
  19. wwwrootPath = wwwrootPath,
  20. OnConfigure = OnConfigure,
  21. urls = urls
  22. });
  23. }
  24. public static void Run(RunArg arg)
  25. {
  26. Action<IServiceCollection> OnConfigureServices = null;
  27. Action<IApplicationBuilder> OnConfigure = null;
  28. #region (x.1)允许跨域访问
  29. if (arg.allowAnyOrigin)
  30. {
  31. OnConfigureServices += (services) =>
  32. {
  33. services.AddCors();
  34. };
  35. OnConfigure += (app) =>
  36. {
  37. app.UseCors(builder => builder
  38. .AllowAnyOrigin()
  39. .AllowAnyMethod()
  40. .AllowAnyHeader()
  41. .AllowCredentials());
  42. };
  43. }
  44. #endregion
  45. #region (x.2)UseStaticFiles
  46. if (!string.IsNullOrWhiteSpace(arg.wwwrootPath) || arg.OnInitStaticFileOptions != null)
  47. {
  48. OnConfigure += (app) =>
  49. {
  50. var staticfileOptions = new StaticFileOptions();
  51. if (!string.IsNullOrWhiteSpace(arg.wwwrootPath))
  52. {
  53. staticfileOptions.FileProvider = new PhysicalFileProvider(arg.wwwrootPath);
  54. }
  55. arg.OnInitStaticFileOptions?.Invoke(staticfileOptions);
  56. app.UseStaticFiles(staticfileOptions);
  57. };
  58. }
  59. #endregion
  60. #region test OnConfigure
  61. //OnConfigure += (app) =>{
  62. // app.Use(async (context, next) =>
  63. // {
  64. // await context.Response.WriteAsync("进入第一个委托 执行下一个委托之前\r\n");
  65. // //调用管道中的下一个委托
  66. // await next.Invoke();
  67. // await context.Response.WriteAsync("结束第一个委托 执行下一个委托之后\r\n");
  68. // });
  69. // app.Run(async (context) =>
  70. // {
  71. // await context.Response.WriteAsync("hello, here is from netcore.");
  72. // });
  73. //};
  74. #endregion
  75. #region (x.3) Build host
  76. OnConfigureServices += arg.OnConfigureServices;
  77. OnConfigure += arg.OnConfigure;
  78. var host =
  79. arg.OnCreateWebHostBuilder().
  80. UseUrls(arg.urls).
  81. ConfigureServices(OnConfigureServices).
  82. Configure(OnConfigure).
  83. Build();
  84. #endregion
  85. #region (x.4) Run
  86. if (arg.RunAsync)
  87. {
  88. host.RunAsync();
  89. }
  90. else
  91. {
  92. host.Run();
  93. }
  94. #endregion
  95. }
  96. #endregion
  97. }
  98. }