Startup.cs 6.4 KB

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