224 lines
8.3 KiB
C#
Raw Normal View History

2026-01-10 13:47:54 +08:00
using AspNetCoreRateLimit;
using Infrastructure.Converter;
2026-01-20 16:11:20 +08:00
using MDM;
2026-01-10 13:47:54 +08:00
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Localization;
2026-01-25 09:45:29 +08:00
using Microsoft.Extensions.Options;
2026-01-10 13:47:54 +08:00
using NLog.Web;
using RIZO.Admin.WebApi.Extensions;
2026-01-20 16:11:20 +08:00
using RIZO.Admin.WebApi.PLC;
using RIZO.Admin.WebApi.PLC.Model;
using RIZO.Admin.WebApi.PLC.Service;
using RIZO.Admin.WebApi.PLC.Service.IService;
2026-01-10 13:47:54 +08:00
using RIZO.Common.Cache;
using RIZO.Common.DynamicApiSimple.Extens;
using RIZO.Infrastructure.WebExtensions;
2026-01-20 16:11:20 +08:00
using RIZO.Mall;
2026-01-28 19:26:37 +08:00
using RIZO.Service.PLCBackground.Stations.Into;
using RIZO.Service.PLCBackground.Stations.Out;
2026-01-10 13:47:54 +08:00
using RIZO.ServiceCore.Signalr;
using RIZO.ServiceCore.SqlSugar;
2026-01-25 09:45:29 +08:00
using S7.Net;
2026-01-20 16:11:20 +08:00
using SqlSugar;
using System.Globalization;
using System.Text.Json;
2026-01-10 13:47:54 +08:00
//using SQLitePCL;
var builder = WebApplication.CreateBuilder(args);
// NLog: Setup NLog for Dependency injection
//builder.Logging.ClearProviders();
builder.Host.UseNLog();
builder.Services.AddDynamicApi();
// Add services to the container.
builder.Services.AddControllers();
2026-01-17 10:42:02 +08:00
// ===== 新增PLC服务注册 =====
2026-01-25 13:54:38 +08:00
builder.Services.Configure<List<PlcConfig>>(builder.Configuration.GetSection("PlcConfigs"));
builder.Services.Configure<GlobalPlcConfig>(builder.Configuration.GetSection("GlobalPlcConfig"));
2026-01-25 20:56:26 +08:00
// 注册PLC轮询配置
builder.Services.Configure<PlcPollingSettings>(builder.Configuration.GetSection("PlcPollingSettings"));
2026-01-25 13:54:38 +08:00
builder.Services.AddSingleton<PlcService>();
builder.Services.AddScoped<IPlcProductionDataService, PlcProductionDataService>();
2026-01-17 10:42:02 +08:00
// 新增注册PLC后台监听服务项目启动自动执行
2026-02-01 14:10:08 +08:00
builder.Services.AddHostedService<PlcHostedService>();
2026-01-17 10:42:02 +08:00
// ==========================
2026-01-10 13:47:54 +08:00
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//注入HttpContextAccessor
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// 跨域配置
builder.Services.AddCors(builder.Configuration);
//消除Error unprotecting the session cookie警告
builder.Services.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
//普通验证码
builder.Services.AddCaptcha(builder.Configuration);
//IPRatelimit
builder.Services.AddIPRate(builder.Configuration);
//builder.Services.AddSession();
builder.Services.AddHttpContextAccessor();
//绑定整个对象到Model上
builder.Services.Configure<OptionsSetting>(builder.Configuration);
builder.Configuration.AddJsonFile("codeGen.json");
builder.Configuration.AddJsonFile("iprate.json");
//jwt 认证
builder.Services.AddJwt();
//配置文件
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
//app服务注册
builder.Services.AddAppService();
//开启计划任务
builder.Services.AddTaskSchedulers();
//请求大小限制
builder.Services.AddRequestLimit(builder.Configuration);
//sqlite 包需要的驱动
//Batteries_V2.Init();
//注册REDIS 服务
var openRedis = builder.Configuration["RedisServer:open"];
if (openRedis == "1")
{
RedisServer.Initalize();
}
builder.Services.AddMvc(options =>
{
options.Filters.Add(typeof(GlobalActionMonitor));//全局注册
})
.AddJsonOptions(options =>
{
//options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString;
options.JsonSerializerOptions.WriteIndented = true;
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
options.JsonSerializerOptions.Converters.Add(new StringConverter());
//PropertyNamingPolicy属性用于前端传过来的属性的格式策略目前内置的仅有一种策略CamelCase
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
//options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;//属性可以忽略大小写格式,开启后性能会降低
});
//注入SignalR实时通讯默认用json传输
builder.Services.AddSignalR()
.AddJsonProtocol(options =>
{
options.PayloadSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
builder.Services.AddSwaggerConfig();
// 显示logo
builder.Services.AddLogo();
// 添加本地化服务
builder.Services.AddLocalization(options => options.ResourcesPath = "");
2026-01-25 09:45:29 +08:00
2026-01-29 11:18:35 +08:00
//PLC进站后台服务注册
2026-01-28 19:52:03 +08:00
//builder.Services.AddHostedService<PlcOutStationService_OP07_01>();
2026-02-03 17:28:39 +08:00
builder.Services.AddHostedService< PlcIntoStationService_OP020_2>();
builder.Services.AddHostedService<PlcIntoStationService_OP020_3>();
builder.Services.AddHostedService<PlcIntoStationService_OP020_4>();
builder.Services.AddHostedService<PlcIntoStationService_OP050>();
builder.Services.AddHostedService<PlcIntoStationService_OP050_1>();
builder.Services.AddHostedService<PlcIntoStationService_OP051Scan>();
2026-02-02 14:08:46 +08:00
builder.Services.AddHostedService<PlcIntoStationService_OP057>();
builder.Services.AddHostedService<PlcIntoStationService_OP058>();
builder.Services.AddHostedService<PlcIntoStationService_OP060>();
2026-02-01 14:10:08 +08:00
builder.Services.AddHostedService<PlcIntoStationService_OP100>();
2026-02-02 14:08:46 +08:00
builder.Services.AddHostedService<PlcIntoStationService_OP102>();
builder.Services.AddHostedService<PlcIntoStationService_OP110_1>();
builder.Services.AddHostedService<PlcIntoStationService_OP110_2>();
builder.Services.AddHostedService<PlcIntoStationService_OP110_3>();
2026-02-03 17:28:39 +08:00
builder.Services.AddHostedService<PlcIntoStationService_OP115>();
2026-01-25 09:45:29 +08:00
2026-01-30 09:24:53 +08:00
//螺丝枪服务注册
//builder.Services.AddHostedService<PF6ScrewGunService>();
2026-01-10 13:47:54 +08:00
// 在应用程序启动的最开始处调用
var app = builder.Build();
InternalApp.ServiceProvider = app.Services;
InternalApp.Configuration = builder.Configuration;
InternalApp.WebHostEnvironment = app.Environment;
//初始化db
builder.Services.AddDb(app.Environment);
builder.Services.InitDb(app.Environment);
2026-01-16 10:40:58 +08:00
//初始化模型db
builder.Services.InitModelDb(app.Environment);
2026-01-10 13:47:54 +08:00
var workId = builder.Configuration["workId"].ParseToInt();
if (app.Environment.IsDevelopment())
{
workId += 1;
}
SnowFlakeSingle.WorkId = workId;
//使用全局异常中间件
app.UseMiddleware<GlobalExceptionMiddleware>();
// 配置中间件以支持本地化
var supportedCultures = new List<CultureInfo> {
new CultureInfo("zh-Hant"),
new CultureInfo("zh-CN"),
new CultureInfo("en")
};
app.UseRequestLocalization(options =>
{
options.DefaultRequestCulture = new RequestCulture("zh-CN");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.FallBackToParentCultures = true;
});
//请求头转发
//ForwardedHeaders中间件会自动把反向代理服务器转发过来的X-Forwarded-For客户端真实IP以及X-Forwarded-Proto客户端请求的协议自动填充到HttpContext.Connection.RemoteIPAddress和HttpContext.Request.Scheme中这样应用代码中读取到的就是真实的IP和真实的协议了不需要应用做特殊处理。
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor | Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto
});
app.Use((context, next) =>
{
//设置可以多次获取body内容
context.Request.EnableBuffering();
if (context.Request.Query.TryGetValue("access_token", out var token))
{
context.Request.Headers.Append("Authorization", $"Bearer {token}");
}
return next();
});
//开启访问静态文件/wwwroot目录文件要放在UseRouting前面
app.UseStaticFiles();
//开启路由访问
app.UseRouting();
app.UseCors("Policy");//要放在app.UseEndpoints前。
//app.UseHttpsRedirection();
//app.UseAuthentication();
app.UseMiddleware<JwtAuthMiddleware>();
app.UseAuthorization();
//开启缓存
app.UseResponseCaching();
if (builder.Environment.IsProduction())
{
//恢复/启动任务
app.UseAddTaskSchedulers();
}
//初始化字典数据
app.UseInit();
//swagger 只在开发环境中使用
if (builder.Environment.IsDevelopment())
{
app.UseSwagger();
}
//启用客户端IP限制速率
app.UseIpRateLimiting();
app.UseRateLimiter();
//设置socket连接
app.MapHub<MessageHub>("/msgHub");
//app.MapControllerRoute(
// name: "default",
// pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapControllers();
app.Run();