Files
number_zj/20220330_Vote/Ewide.Web.Core/Startup.cs
2023-06-28 23:07:04 +08:00

118 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Ewide.Core;
using Furion;
using Furion.DependencyInjection;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OnceMi.AspNetCore.OSS;
using Serilog;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Ewide.Web.Core
{
public class Startup : AppStartup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddJwt<JwtHandler>(enableGlobalAuthorize: true, jwtBearerConfigure: config =>
{
//image 文件 等浏览器相关操作时
config.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
if (context.Request.Query.ContainsKey("access_token"))
{
context.Token = context.Request.Query["access_token"];
}
return Task.CompletedTask;
}
};
});
services.AddCorsAccessor();
services.AddRemoteRequest();
services.AddConfigurableOptions<CacheOptions>();
services.AddConfigurableOptions<UploadFileOptions>();
services.AddControllersWithViews()
.AddMvcFilter<RequestActionFilter>()
.AddInjectWithUnifyResult<RestfulResultProvider>()
// 在管道中增加NewtonsoftJson,防止参数类型严格验证
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
})
.AddJsonOptions(options =>
{
//options.JsonSerializerOptions.DefaultBufferSize = 10_0000;//返回较大数据数据序列化时会截断原因默认缓冲区大小以字节为单位为16384。
//options.JsonSerializerOptions.Converters.AddDateFormatString("yyyy-MM-dd HH:mm:ss");
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; // 忽略循环引用 仅.NET 6支持
});
services.AddViewEngine();
//services.AddEventBridge();
services.AddEventBus(builder =>
{
builder.AddSubscriber<Ewide.Core.EventHandlers.LogEventHandler>();
});
// services.AddLocalization();
// 设置雪花id的workerId确保每个实例workerId都应不同
//var workerId = ushort.Parse(App.Configuration["SnowId:WorkerId"] ?? "1");
//IDGenerator.SetIdGenerator(new IDGeneratorOptions { WorkerId = workerId });
services.AddOSSService("HuaweiCloud", "OSSProvider");
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
// 添加状态码拦截中间件
app.UseUnifyResultStatusCodes();
//app.UseHttpsRedirection(); // 强制https
app.UseStaticFiles();
// Serilog请求日志中间件---必须在 UseStaticFiles 和 UseRouting 之间
app.UseSerilogRequestLogging();
//app.UseAppLocalization();
app.UseRouting();
app.UseCorsAccessor();
app.UseAuthentication();
app.UseAuthorization();
app.UseInject();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "gb/yjb/{controller=Home}/{action=Index}/{id?}");
});
}
}
}