From 991d8012c2f6d805467f516e14dfd8669e5f053c Mon Sep 17 00:00:00 2001 From: JerryMouseLi <13737732703@163.com> Date: Wed, 27 Mar 2019 11:55:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=94=9F=E5=91=BD?= =?UTF-8?q?=E5=91=A8=E6=9C=9F=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 数据库生命周期更改成每一次请求就结束,而不是应用程序的整个生命周期 --- Startup.cs | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Startup.cs diff --git a/Startup.cs b/Startup.cs new file mode 100644 index 0000000..6c04520 --- /dev/null +++ b/Startup.cs @@ -0,0 +1,94 @@ +namespace APIJSON.NET +{ + using System; + using System.Collections.Generic; + using System.Text; + using APIJSON.NET.Models; + using APIJSON.NET.Services; + using Microsoft.AspNetCore.Builder; + using Microsoft.AspNetCore.Hosting; + using Microsoft.AspNetCore.Http; + using Microsoft.AspNetCore.Mvc; + using Microsoft.Extensions.Configuration; + using Microsoft.Extensions.DependencyInjection; + using Microsoft.IdentityModel.Tokens; + using SqlKata.Execution; + using Swashbuckle.AspNetCore.Swagger; + using MySql.Data.MySqlClient; + using SqlKata.Compilers; + + public class Startup + { + private const string _defaultCorsPolicyName = "localhost"; + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + 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.Configure>(Configuration.GetSection("RoleList")); + services.Configure>(Configuration.GetSection("tablempper")); + services.Configure(tokenAuthConfig => + { + tokenAuthConfig.SecurityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["Authentication:JwtBearer:SecurityKey"])); + tokenAuthConfig.Issuer = Configuration["Authentication:JwtBearer:Issuer"]; + tokenAuthConfig.Audience = Configuration["Authentication:JwtBearer:Audience"]; + tokenAuthConfig.SigningCredentials = new SigningCredentials(tokenAuthConfig.SecurityKey, SecurityAlgorithms.HmacSha256); + tokenAuthConfig.Expiration = TimeSpan.FromDays(1); + }); + AuthConfigurer.Configure(services, Configuration); + + services.AddCors( options => options.AddPolicy( _defaultCorsPolicyName, + builder => + builder.AllowAnyOrigin() + .AllowAnyHeader() + .AllowAnyMethod().AllowCredentials() + )); + services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new Info { Title = "APIJSON.NET", Version = "v1" }); + }); + // services.AddSingleton(); + //services.AddSingleton(); + services.AddScoped(); + services.AddScoped(); + + services.AddSingleton(); + services.AddSingleton(); + services.AddTransient(); + services.AddTransient(); + + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + + app.UseAuthentication(); + + app.UseMvc(routes => + { + routes.MapRoute( + name: "default", + template: "{controller=Home}/{action=Index}/{id?}"); + }); + app.UseStaticFiles(); + app.UseCors(_defaultCorsPolicyName); + app.UseSwagger(); + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); + + }); + + app.UseJwtTokenMiddleware(); + DbInit.Initialize(app); + } + } +}