Startup.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.AspNetCore.Authentication.Cookies;
  6. using ZcPeng.PublicLibrary;
  7. using System.Text;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.EntityFrameworkCore;
  10. using SupplierWeb.Codes.Mvc;
  11. using Microsoft.Extensions.FileProviders;
  12. using System.IO;
  13. using log4net;
  14. using log4net.Config;
  15. using log4net.Repository;
  16. using Newtonsoft.Json.Serialization;
  17. using System;
  18. using Microsoft.Extensions.Caching.Memory;
  19. using SupplierWeb.Service;
  20. namespace SupplierWeb
  21. {
  22. public class Startup
  23. {
  24. public static ILoggerRepository Repository;
  25. public Startup(IConfiguration configuration)
  26. {
  27. Configuration = configuration;
  28. Repository = LogManager.CreateRepository("NETCoreRepository");
  29. XmlConfigurator.Configure(Repository, new FileInfo("log4net.config"));
  30. //OrderConfirmTimedProgram.RunProgram("0", "*", "*", "1", "1", "", null);
  31. }
  32. public IConfiguration Configuration { get; }
  33. // This method gets called by the runtime. Use this method to add services to the container.
  34. public void ConfigureServices(IServiceCollection services)
  35. {
  36. // services.AddHttpService();
  37. //services.Configure<MvcOptions>(options =>
  38. //{
  39. // options.InputFormatters.Add(new WechatXmlSerializerInputFormatter());
  40. //});
  41. services.AddWXFramework();
  42. services.AddMemoryCache();
  43. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();//简化了访问HttpContext
  44. services.AddSingleton<IMemoryCache>(factory =>
  45. {
  46. var cache = new MemoryCache(new MemoryCacheOptions());
  47. return cache;
  48. });
  49. services.AddSingleton<IEsClientProvider, EsClientProvider>();
  50. services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
  51. .AddCookie(options =>
  52. {
  53. //options.AccessDeniedPath = "/login";
  54. options.LoginPath = "/login";
  55. });
  56. //services.AddMvc();
  57. services.AddMvc(config => config.Filters.Add(typeof(MyGlobalActionFilter)))
  58. //Asp.net Core WebApi 返回JSON自动驼峰格式化问题:
  59. .AddJsonOptions(options => {
  60. options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
  61. }); ; ;
  62. //services.AddMvc(options =>
  63. //options.Filters.Remove(new AutoValidateAntiforgeryTokenAttribute()));
  64. services.AddSession();
  65. //services.AddCors(options => {
  66. // options.AddPolicy("CorsSample", p =>
  67. // p.WithOrigins("http://localhost:8000", "http://*.360lj.com")
  68. // .AllowAnyMethod()
  69. // .AllowAnyHeader()
  70. // //.WithHeaders("authorization", "accept", "content-type", "origin")
  71. // //.AllowCredentials()
  72. // //"Access-Control-Allow-Origin *",
  73. // //"Access-Control-Allow-Headers DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type",
  74. // //"Access-Control-Allow-Methods GET,PUT,POST,DELETE,OPTIONS"
  75. // );
  76. //}
  77. //);
  78. //Add TimedJob services
  79. services.AddTimedJob();
  80. var connection = @"Data Source =192.168.50.30; Initial Catalog = LJHYBZK; Persist Security Info = True; User ID = sa; Password = xq!@#2014;";
  81. //services.AddDbContext<LJHYBZKContext>(options => options.UseSqlServer(connection));
  82. }
  83. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  84. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider svp)
  85. {
  86. //使用TimedJob
  87. app.UseTimedJob();
  88. if (env.IsDevelopment())
  89. {
  90. app.UseDeveloperExceptionPage();
  91. app.UseBrowserLink();
  92. }
  93. else
  94. {
  95. //app.UseExceptionHandler("/Error");
  96. app.UseStatusCodePages(async context =>
  97. {
  98. var resp = context.HttpContext.Response;
  99. resp.ContentType = "text/plain";
  100. if (resp.StatusCode == 404)
  101. {
  102. //需要 using Microsoft.AspNetCore.Http;
  103. await resp.WriteAsync($"当前是404页面,暂时没有获取到异常内容", Encoding.UTF8);
  104. }
  105. else if (resp.StatusCode == 500)
  106. {
  107. await resp.WriteAsync($"当前是500页面,暂时没有获取到异常内容", Encoding.UTF8);
  108. }
  109. });
  110. //app.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
  111. }
  112. app.UseStaticFiles();
  113. //app.UseStaticFiles(new StaticFileOptions
  114. //{
  115. // FileProvider = new PhysicalFileProvider(
  116. // Path.Combine(Directory.GetCurrentDirectory(), "UploadFiles")),
  117. // RequestPath = "/UploadFiles"
  118. //});
  119. app.UseAuthentication();
  120. app.UseSession();
  121. app.UseMvc(routes =>
  122. {
  123. routes.MapRoute(
  124. name: "default",
  125. template: "{controller}/{action=Index}");
  126. });
  127. //app.UseCors("CorsSample");
  128. //Common.Http.MyHttpContext.ServiceProvider = svp;
  129. Common.Http.MyHttpContext.Configure(app.ApplicationServices.
  130. GetRequiredService<Microsoft.AspNetCore.Http.IHttpContextAccessor>()
  131. );
  132. Common.Http.MyHttpContext.Configure(app.ApplicationServices.
  133. GetRequiredService<IMemoryCache>()
  134. );
  135. }
  136. }
  137. }