123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.AspNetCore.Authentication.Cookies;
- using ZcPeng.PublicLibrary;
- using System.Text;
- using Microsoft.AspNetCore.Http;
- using Microsoft.EntityFrameworkCore;
- using SupplierWeb.Codes.EF;
- using SupplierWeb.Codes.Mvc;
- using Microsoft.Extensions.FileProviders;
- using System.IO;
- using log4net;
- using log4net.Config;
- using log4net.Repository;
- using Newtonsoft.Json.Serialization;
- using System;
- using Microsoft.Extensions.Caching.Memory;
- using CoreEntity.TimedTask;
- namespace SupplierWeb
- {
- public class Startup
- {
- public static ILoggerRepository Repository;
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- Repository = LogManager.CreateRepository("NETCoreRepository");
- XmlConfigurator.Configure(Repository, new FileInfo("log4net.config"));
- //OrderConfirmTimedProgram.RunProgram("0", "*", "*", "1", "1", "", null);
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- // services.AddHttpService();
- //services.Configure<MvcOptions>(options =>
- //{
- // options.InputFormatters.Add(new WechatXmlSerializerInputFormatter());
- //});
- services.AddWXFramework();
- services.AddMemoryCache();
- services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();//简化了访问HttpContext
- services.AddSingleton<IMemoryCache>(factory =>
- {
- var cache = new MemoryCache(new MemoryCacheOptions());
- return cache;
- });
- services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
- .AddCookie(options =>
- {
- //options.AccessDeniedPath = "/login";
- options.LoginPath = "/login";
- });
- //services.AddMvc();
- services.AddMvc(config => config.Filters.Add(typeof(MyGlobalActionFilter)))
- //Asp.net Core WebApi 返回JSON自动驼峰格式化问题:
- ;//.AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); ; ;
- //services.AddMvc(options =>
- //options.Filters.Remove(new AutoValidateAntiforgeryTokenAttribute()));
- services.AddSession();
- //services.AddCors(options => {
- // options.AddPolicy("CorsSample", p =>
- // p.WithOrigins("http://localhost:8000", "http://*.360lj.com")
- // .AllowAnyMethod()
- // .AllowAnyHeader()
- // //.WithHeaders("authorization", "accept", "content-type", "origin")
- // //.AllowCredentials()
- // //"Access-Control-Allow-Origin *",
- // //"Access-Control-Allow-Headers DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type",
- // //"Access-Control-Allow-Methods GET,PUT,POST,DELETE,OPTIONS"
- // );
- //}
- //);
- //Add TimedJob services
- services.AddTimedJob();
- var connection = @"Data Source =192.168.50.30; Initial Catalog = LJHYBZK; Persist Security Info = True; User ID = sa; Password = xq!@#2014;";
- services.AddDbContext<LJHYBZKContext>(options => options.UseSqlServer(connection));
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider svp)
- {
- //使用TimedJob
- app.UseTimedJob();
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- app.UseBrowserLink();
- }
- else
- {
- //app.UseExceptionHandler("/Error");
- app.UseStatusCodePages(async context =>
- {
- var resp = context.HttpContext.Response;
- resp.ContentType = "text/plain";
- if (resp.StatusCode == 404)
- {
- //需要 using Microsoft.AspNetCore.Http;
- await resp.WriteAsync($"当前是404页面,暂时没有获取到异常内容", Encoding.UTF8);
- }
- else if (resp.StatusCode == 500)
- {
- await resp.WriteAsync($"当前是500页面,暂时没有获取到异常内容", Encoding.UTF8);
- }
- });
- //app.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
- }
- app.UseStaticFiles();
- //app.UseStaticFiles(new StaticFileOptions
- //{
- // FileProvider = new PhysicalFileProvider(
- // Path.Combine(Directory.GetCurrentDirectory(), "UploadFiles")),
- // RequestPath = "/UploadFiles"
- //});
- app.UseAuthentication();
- app.UseSession();
- app.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller}/{action=Index}");
- });
- //app.UseCors("CorsSample");
- //Common.Http.MyHttpContext.ServiceProvider = svp;
- Common.Http.MyHttpContext.Configure(app.ApplicationServices.
- GetRequiredService<Microsoft.AspNetCore.Http.IHttpContextAccessor>()
- );
- Common.Http.MyHttpContext.Configure(app.ApplicationServices.
- GetRequiredService<IMemoryCache>()
- );
- }
- }
- }
|