Startup.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.EF;
  11. using SupplierWeb.Codes.Mvc;
  12. using Microsoft.Extensions.FileProviders;
  13. using System.IO;
  14. using log4net;
  15. using log4net.Config;
  16. using log4net.Repository;
  17. using Newtonsoft.Json.Serialization;
  18. using System;
  19. using Microsoft.Extensions.Caching.Memory;
  20. using CoreEntity.TimedTask;
  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.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 => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); ; ;
  60. //services.AddMvc(options =>
  61. //options.Filters.Remove(new AutoValidateAntiforgeryTokenAttribute()));
  62. services.AddSession();
  63. //services.AddCors(options => {
  64. // options.AddPolicy("CorsSample", p =>
  65. // p.WithOrigins("http://localhost:8000", "http://*.360lj.com")
  66. // .AllowAnyMethod()
  67. // .AllowAnyHeader()
  68. // //.WithHeaders("authorization", "accept", "content-type", "origin")
  69. // //.AllowCredentials()
  70. // //"Access-Control-Allow-Origin *",
  71. // //"Access-Control-Allow-Headers DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type",
  72. // //"Access-Control-Allow-Methods GET,PUT,POST,DELETE,OPTIONS"
  73. // );
  74. //}
  75. //);
  76. //Add TimedJob services
  77. services.AddTimedJob();
  78. var connection = @"Data Source =192.168.50.30; Initial Catalog = LJHYBZK; Persist Security Info = True; User ID = sa; Password = xq!@#2014;";
  79. services.AddDbContext<LJHYBZKContext>(options => options.UseSqlServer(connection));
  80. }
  81. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  82. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider svp)
  83. {
  84. //使用TimedJob
  85. app.UseTimedJob();
  86. if (env.IsDevelopment())
  87. {
  88. app.UseDeveloperExceptionPage();
  89. app.UseBrowserLink();
  90. }
  91. else
  92. {
  93. //app.UseExceptionHandler("/Error");
  94. app.UseStatusCodePages(async context =>
  95. {
  96. var resp = context.HttpContext.Response;
  97. resp.ContentType = "text/plain";
  98. if (resp.StatusCode == 404)
  99. {
  100. //需要 using Microsoft.AspNetCore.Http;
  101. await resp.WriteAsync($"当前是404页面,暂时没有获取到异常内容", Encoding.UTF8);
  102. }
  103. else if (resp.StatusCode == 500)
  104. {
  105. await resp.WriteAsync($"当前是500页面,暂时没有获取到异常内容", Encoding.UTF8);
  106. }
  107. });
  108. //app.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
  109. }
  110. app.UseStaticFiles();
  111. //app.UseStaticFiles(new StaticFileOptions
  112. //{
  113. // FileProvider = new PhysicalFileProvider(
  114. // Path.Combine(Directory.GetCurrentDirectory(), "UploadFiles")),
  115. // RequestPath = "/UploadFiles"
  116. //});
  117. app.UseAuthentication();
  118. app.UseSession();
  119. app.UseMvc(routes =>
  120. {
  121. routes.MapRoute(
  122. name: "default",
  123. template: "{controller}/{action=Index}");
  124. });
  125. //app.UseCors("CorsSample");
  126. //Common.Http.MyHttpContext.ServiceProvider = svp;
  127. Common.Http.MyHttpContext.Configure(app.ApplicationServices.
  128. GetRequiredService<Microsoft.AspNetCore.Http.IHttpContextAccessor>()
  129. );
  130. Common.Http.MyHttpContext.Configure(app.ApplicationServices.
  131. GetRequiredService<IMemoryCache>()
  132. );
  133. }
  134. }
  135. }