Startup.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using DllLotServer;
  6. using Microsoft.AspNetCore.Builder;
  7. using Microsoft.AspNetCore.Hosting;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.HttpsPolicy;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using Microsoft.Extensions.Logging;
  14. namespace WebLot
  15. {
  16. public class Startup
  17. {
  18. public Startup(IConfiguration configuration)
  19. {
  20. Configuration = configuration;
  21. }
  22. public IConfiguration Configuration { get; }
  23. // This method gets called by the runtime. Use this method to add services to the container.
  24. public void ConfigureServices(IServiceCollection services)
  25. {
  26. services.Configure<CookiePolicyOptions>(options =>
  27. {
  28. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  29. options.CheckConsentNeeded = context => true;
  30. options.MinimumSameSitePolicy = SameSiteMode.None;
  31. });
  32. services.AddSingleton<ILotServer, LotServer>();
  33. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  34. }
  35. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  36. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory factory)
  37. {
  38. ILogger myloger = factory.AddLog4Net().CreateLogger(typeof(Startup));// app.ApplicationServices.GetRequiredService<ILogger<Startup>>();
  39. if (env.IsDevelopment())
  40. {
  41. app.UseDeveloperExceptionPage();
  42. }
  43. else
  44. {
  45. app.UseExceptionHandler("/Home/Error");
  46. app.UseHsts();
  47. }
  48. //获取通讯管理主程序实例
  49. var server = app.ApplicationServices.GetRequiredService<ILotServer>();
  50. string errorinfo = "";
  51. server.Start(ref errorinfo);
  52. if(errorinfo!="")
  53. {
  54. myloger.LogError($"启动服务发生错误:{errorinfo}");
  55. }
  56. else
  57. {
  58. myloger.LogInformation("启动服务成功。");
  59. }
  60. app.UseHttpsRedirection();
  61. app.UseStaticFiles();
  62. app.UseCookiePolicy();
  63. app.UseCors(builder => builder
  64. .AllowAnyOrigin()
  65. .AllowAnyMethod()
  66. .AllowAnyHeader()
  67. .AllowCredentials());
  68. app.UseMvc(routes =>
  69. {
  70. routes.MapRoute(
  71. name: "default",
  72. template: "{controller=Home}/{action=Index}/{id?}");
  73. });
  74. }
  75. }
  76. }