91 lines
3.3 KiB
C#
91 lines
3.3 KiB
C#
using Microsoft.AspNetCore.Builder;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
using Microsoft.AspNetCore.HttpsPolicy;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.Extensions.Configuration;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||
using Microsoft.Extensions.Hosting;
|
||
using Microsoft.Extensions.Http;
|
||
using Microsoft.Extensions.Logging;
|
||
using Microsoft.OpenApi.Models;
|
||
using Swashbuckle.AspNetCore.SwaggerUI;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IdentityModel.Tokens.Jwt;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using WMS.Web.Domain.Options;
|
||
using WMS.Web.Repositories.DependencyInjection;
|
||
|
||
namespace WMS.Web.Api
|
||
{
|
||
public class Startup
|
||
{
|
||
public Startup(IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
|
||
{
|
||
Configuration = configuration;
|
||
WebHostEnvironment = webHostEnvironment;
|
||
}
|
||
|
||
public IConfiguration Configuration { get; }
|
||
|
||
public IWebHostEnvironment WebHostEnvironment;
|
||
|
||
// This method gets called by the runtime. Use this method to add services to the container.
|
||
public void ConfigureServices(IServiceCollection services)
|
||
{
|
||
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
|
||
var option = Configuration.GetSection("AppOptions").Get<AppOptions>();
|
||
var option_soa = Configuration.GetSection("SoaOptions").Get<SoaOptions>();
|
||
|
||
var serverVersion = new MySqlServerVersion(new Version(8, 0, 18));
|
||
//数据库注入
|
||
services.AddApp(Configuration, WebHostEnvironment, builder =>
|
||
{
|
||
builder.AddDbContext(
|
||
opt => { opt.UseMySql(option.DBConnectionString, serverVersion); });
|
||
});
|
||
|
||
services.AddControllers();
|
||
//移除:http请求的默认日志处理器(备注:也可以实现它,自定义自己想要的日志,文档地址https://docs.microsoft.com/zh-cn/dotnet/core/compatibility/aspnet-core/5.0/http-httpclient-instances-log-integer-status-codes)
|
||
services.RemoveAll<IHttpMessageHandlerBuilderFilter>();
|
||
}
|
||
|
||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||
{
|
||
if (env.IsDevelopment())
|
||
{
|
||
app.UseDeveloperExceptionPage();
|
||
}
|
||
if (WebHostEnvironment.IsDevelopment())
|
||
{
|
||
// Swagger中间件
|
||
app.UseSwagger();
|
||
// SwaggerUI
|
||
app.UseSwaggerUI(c =>
|
||
{
|
||
c.SwaggerEndpoint($"/swagger/v1/swagger.json", "WMS-Api");
|
||
c.RoutePrefix = string.Empty;
|
||
c.DocExpansion(DocExpansion.None);
|
||
});
|
||
}
|
||
|
||
app.UseHttpsRedirection();
|
||
app.UseStaticFiles();
|
||
app.UseCors("AllowAllOrigin");
|
||
//统一异常处理中间件
|
||
app.UseMiddleware<AuthorizationTokenSecurityPolicy>();
|
||
app.UseMiddleware<ErrorHandlingMiddleware>();
|
||
app.UseRouting();
|
||
app.UseEndpoints(endpoints =>
|
||
{
|
||
endpoints.MapControllers();
|
||
});
|
||
}
|
||
}
|
||
}
|