Startup.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Cksoft.Unity;
  2. using DllEapCommon.Filters;
  3. using DllEapFileUpload;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.AspNetCore.Mvc.ApplicationParts;
  9. using Microsoft.AspNetCore.Mvc.Controllers;
  10. using Microsoft.AspNetCore.Mvc.Formatters;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using Microsoft.Extensions.Logging;
  14. using System;
  15. using System.Linq;
  16. using System.Text;
  17. using WebApplet.Filters;
  18. using WebApplet.ServiceExtensions;
  19. namespace WebApplet
  20. {
  21. public class Startup
  22. {
  23. public Startup(IConfiguration configuration)
  24. {
  25. Configuration = configuration;
  26. }
  27. public IConfiguration Configuration { get; }
  28. // This method gets called by the runtime. Use this method to add services to the container.
  29. public void ConfigureServices(IServiceCollection services)
  30. {
  31. services.Configure<CookiePolicyOptions>(options =>
  32. {
  33. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  34. options.CheckConsentNeeded = context => true;
  35. options.MinimumSameSitePolicy = SameSiteMode.None;
  36. });
  37. Action<MvcOptions> filters = new Action<MvcOptions>(r =>
  38. {
  39. // r.Filters.Add(typeof(WebApiResultMiddleware)); // 格式化 ADT系统 返回数据
  40. r.Filters.Add(typeof(ExceptionFilterFor));
  41. //r.Filters.Add(typeof(ButtonFilter));
  42. r.Filters.Add(typeof(QueryStringFilter));
  43. // 设置接收和返回值可以为XML格式
  44. r.InputFormatters.Add(new XmlSerializerInputFormatter(new MvcOptions { }));
  45. r.OutputFormatters.Add(new XmlSerializerOutputFormatter());
  46. });
  47. services.AddMvc(filters).SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
  48. .ConfigureApplicationPartManager(m =>{ });
  49. var manager = new ApplicationPartManager();
  50. manager.FeatureProviders.Add(new ControllerFeatureProvider());
  51. var feature = new ControllerFeature();
  52. manager.PopulateFeature(feature);
  53. services.AddSingleton(feature.Controllers.Select(t => t.AsType()).ToArray());
  54. AppConfigurtaionServices.Instance = services.BuildServiceProvider();
  55. services.AddCors(options =>
  56. {
  57. options.AddPolicy("all", builder =>
  58. {
  59. builder.AllowAnyOrigin() //允许任何来源的主机访问
  60. .AllowAnyMethod()
  61. .AllowAnyHeader()
  62. .AllowCredentials();//指定处理cookie
  63. });
  64. });
  65. services.AddNSwag();
  66. services.AddSingleton<IEapScoketServer, EapSocketServer>();
  67. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  68. }
  69. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  70. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory factory)
  71. {
  72. NLog.ILogger logger = NLog.LogManager.LoadConfiguration("NLog.config").GetCurrentClassLogger();
  73. NLog.LogManager.Configuration.Variables["connectionString"] = Configuration["eap:ConnectionStrings"];
  74. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); //避免日志中的中文输出乱码
  75. ILogger myloger = factory.AddLog4Net().CreateLogger(typeof(Startup));
  76. if (env.IsDevelopment())
  77. {
  78. app.UseDeveloperExceptionPage();
  79. }
  80. else
  81. {
  82. app.UseExceptionHandler("/Error");
  83. }
  84. string errorinfo = "";
  85. myloger.LogError($"启动服务成功:{errorinfo}");
  86. if (env.IsDevelopment())
  87. {
  88. app.UseDeveloperExceptionPage();
  89. }
  90. else
  91. {
  92. app.UseExceptionHandler("/Home/Error");
  93. app.UseHsts();
  94. }
  95. app.UseStaticFiles();
  96. app.UseCookiePolicy();
  97. app.UseNSwag();
  98. app.UseCors(builder => builder
  99. .AllowAnyOrigin()
  100. .AllowAnyMethod()
  101. .AllowAnyHeader()
  102. .AllowCredentials());
  103. app.UseMvc(routes =>
  104. {
  105. routes.MapRoute(
  106. name: "default",
  107. template: "{controller=Home}/{action=Index}/{id?}");
  108. });
  109. if (Configuration["EapSocket"] != null && Convert.ToBoolean(Configuration["EapSocket"]) == true)
  110. {
  111. var EapScoket = app.ApplicationServices.GetRequiredService<IEapScoketServer>();
  112. errorinfo = "";
  113. EapScoket.Start(ref errorinfo);
  114. myloger.LogError(errorinfo);
  115. }
  116. //app.UseHttpsRedirection();
  117. //app.UseStaticFiles();
  118. //app.UseCookiePolicy();
  119. //app.UseMvc(routes =>
  120. //{
  121. // routes.MapRoute(
  122. // name: "default",
  123. // template: "{controller=Home}/{action=Index}/{id?}");
  124. //});
  125. }
  126. }
  127. }