Startup.cs 5.8 KB

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