using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; 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.Hosting; using Microsoft.Extensions.Logging; using Pomelo.EntityFrameworkCore.MySql; using Haoliang.Core.Services; using Haoliang.Api.Middleware; using Haoliang.Api.Hubs; using Haoliang.Data; using Haoliang.Data.Entities; using Haoliang.Data.Repositories; namespace Haoliang.Api { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "CNC Data Collection API", Version = "v1" }); }); // 配置跨域 services.AddCors(options => { options.AddPolicy("AllowAll", builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); }); // 配置SignalR services.AddSignalR(); // 配置服务依赖注入 RegisterServices(services); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "CNC Data Collection API v1")); } // 中间件顺序很重要 app.UseStaticFiles(); app.UseMiddleware(); app.UseCors("AllowAll"); app.UseMiddleware(); app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapHub("/realtime"); }); } private void RegisterServices(IServiceCollection services) { // 配置数据库连接 var connectionString = Configuration.GetConnectionString("DefaultConnection"); var businessConnectionString = Configuration.GetConnectionString("CNCBusinessDB"); // 业务数据库配置 if (!string.IsNullOrEmpty(businessConnectionString)) { services.AddDbContext(options => options.UseMySql(businessConnectionString, ServerVersion.AutoDetect(businessConnectionString), mysqlOptions => mysqlOptions.EnableRetryOnFailure( maxRetryCount: 3, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null))); } // 日志数据库配置 var logConnectionString = Configuration.GetConnectionString("CNCLLogDB"); if (!string.IsNullOrEmpty(logConnectionString)) { services.AddDbContext(options => options.UseMySql(logConnectionString, ServerVersion.AutoDetect(logConnectionString), mysqlOptions => mysqlOptions.EnableRetryOnFailure( maxRetryCount: 3, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null))); } // 注册主要数据库上下文(用于业务操作) // 注意:DbContext 在下方统一注册一次,避免重复实例化 // 配置内存缓存 services.AddMemoryCache(); services.AddDistributedMemoryCache(); // 注册缓存服务(使用Scoped生命周期以支持依赖注入) services.AddScoped(); // 注册状态机服务 services.AddSingleton(); services.AddHostedService(sp => sp.GetRequiredService()); // 注册服务 services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // 注册配置服务 services.AddScoped(); services.AddScoped(); // 注册中间件服务 services.AddScoped(); // 注册后台任务服务 services.AddHostedService(); // 注册仓储 (仅注册存在的) services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // 注册Ping服务 services.AddScoped(); // 注册数据解析和存储服务 services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); // 注册认证和授权服务 services.AddScoped(); services.AddScoped(); services.AddScoped(); // 注册SignalR Hub services.AddSingleton(); } } }