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.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 SupplierWeb.Service; //using Pivotal.Discovery.Client; 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(options => //{ // options.InputFormatters.Add(new WechatXmlSerializerInputFormatter()); //}); services.AddWXFramework(); services.AddMemoryCache(); services.AddSingleton();//简化了访问HttpContext services.AddSingleton(factory => { var cache = new MemoryCache(new MemoryCacheOptions()); return cache; }); services.AddSingleton(); 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 Newtonsoft.Json.Serialization.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 connectionErp = Configuration["ConnectionStrings:MyConnectionString"]; DataAccess.connectionString = connectionErp; //services.AddDbContext(options => options.UseSqlServer(connection)); // Add Steeltoe Discovery Client service //services.AddDiscoveryClient(Configuration); } // 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() ); Common.Http.MyHttpContext.Configure(app.ApplicationServices. GetRequiredService() ); // Add Steeltoe Discovery Client service //app.UseDiscoveryClient(); } } }