Startup.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using DllBIStatusServer;
  6. using DllEapFileUpload;
  7. using Microsoft.AspNetCore.Builder;
  8. using Microsoft.AspNetCore.Hosting;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.AspNetCore.HttpsPolicy;
  11. using Microsoft.AspNetCore.Mvc;
  12. using Microsoft.Extensions.Configuration;
  13. using Microsoft.Extensions.DependencyInjection;
  14. using Microsoft.Extensions.Logging;
  15. namespace WebMainHsms
  16. {
  17. public class Startup
  18. {
  19. public Startup(IConfiguration configuration)
  20. {
  21. Configuration = configuration;
  22. }
  23. public IConfiguration Configuration { get; }
  24. // This method gets called by the runtime. Use this method to add services to the container.
  25. public void ConfigureServices(IServiceCollection services)
  26. {
  27. services.Configure<CookiePolicyOptions>(options =>
  28. {
  29. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  30. options.CheckConsentNeeded = context => true;
  31. options.MinimumSameSitePolicy = SameSiteMode.None;
  32. });
  33. services.AddSingleton<IBIStatusServer, BIStatusServer>();
  34. //services.AddSingleton<IEapScoketServer, EapSocketServer>();
  35. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  36. }
  37. private string test()
  38. {
  39. try
  40. {
  41. int a = int.Parse("abc");
  42. return "";
  43. }
  44. catch(Exception ex)
  45. {
  46. return $"Exception={ex};"+ex.ToString();
  47. }
  48. }
  49. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  50. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory factory)
  51. {
  52. //string aaa = test();
  53. //Console.WriteLine(aaa);
  54. //return;
  55. ILogger myloger = factory.AddLog4Net().CreateLogger(typeof(Startup));// app.ApplicationServices.GetRequiredService<ILogger<Startup>>();
  56. if (env.IsDevelopment())
  57. {
  58. app.UseDeveloperExceptionPage();
  59. }
  60. else
  61. {
  62. app.UseExceptionHandler("/Home/Error");
  63. app.UseHsts();
  64. }
  65. //获取通讯管理主程序实例
  66. var server = app.ApplicationServices.GetRequiredService<IBIStatusServer>();
  67. string errorinfo = "";
  68. server.Start(ref errorinfo);
  69. if(errorinfo!="")
  70. {
  71. myloger.LogError($"启动服务发生错误:{errorinfo}");
  72. }
  73. else
  74. {
  75. myloger.LogInformation("启动服务成功。");
  76. }
  77. //文件上传下载服务端监听服务
  78. //var socketServer = app.ApplicationServices.GetRequiredService<IEapScoketServer>();
  79. //socketServer.Start(ref errorinfo);
  80. //if (errorinfo != "")
  81. //{
  82. // myloger.LogError($"启动IEapScoketServer服务发生错误:{errorinfo}");
  83. //}
  84. //else
  85. //{
  86. // myloger.LogInformation("启动IEapScoketServer服务成功。");
  87. //}
  88. app.UseHttpsRedirection();
  89. app.UseStaticFiles();
  90. app.UseCookiePolicy();
  91. app.UseFileServer();
  92. app.UseCors(builder => builder
  93. .AllowAnyOrigin()
  94. .AllowAnyMethod()
  95. .AllowAnyHeader()
  96. .AllowCredentials());
  97. app.UseMvc(routes =>
  98. {
  99. routes.MapRoute(
  100. name: "default",
  101. template: "{controller=Home}/{action=Index}/{id?}");
  102. });
  103. }
  104. }
  105. }