You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
7.6 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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.UseMiddleware<LoggingMiddleware>();
app.UseCors("AllowAll");
app.UseMiddleware<ExceptionMiddleware>();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<RealTimeHub>("/realtime");
});
}
private void RegisterServices(IServiceCollection services)
{
// 配置数据库连接
var connectionString = Configuration.GetConnectionString("DefaultConnection");
var businessConnectionString = Configuration.GetConnectionString("BusinessDbConnection");
// 业务数据库配置
if (!string.IsNullOrEmpty(businessConnectionString))
{
services.AddDbContext<CNCBusinessDbContext>(options =>
options.UseMySql(businessConnectionString,
ServerVersion.AutoDetect(businessConnectionString),
mysqlOptions => mysqlOptions.EnableRetryOnFailure(
maxRetryCount: 3,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorNumbersToAdd: null)));
}
// 日志数据库配置
var logConnectionString = Configuration.GetConnectionString("LogDbConnection");
if (!string.IsNullOrEmpty(logConnectionString))
{
services.AddDbContext<CNCLLogDbContext>(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<ICacheService, MemoryCacheService>();
// 注册状态机服务
services.AddSingleton<DeviceStateMachine>();
services.AddHostedService(sp => sp.GetRequiredService<DeviceStateMachine>());
// 注册服务
services.AddScoped<IDeviceCollectionService, DeviceCollectionService>();
services.AddScoped<IProductionService, ProductionService>();
services.AddScoped<IProductionCalculator, ProductionCalculator>();
services.AddScoped<IProductionScheduler, ProductionScheduler>();
services.AddScoped<IAlarmService, AlarmService>();
services.AddScoped<IAlarmRuleService, AlarmRuleService>();
services.AddScoped<IAlarmNotificationService, AlarmNotificationService>();
services.AddScoped<ITemplateService, TemplateService>();
services.AddScoped<ITagMappingService, TagMappingService>();
services.AddScoped<ITemplateValidationService, TemplateValidationService>();
services.AddScoped<ITemplateMigrationService, TemplateMigrationService>();
services.AddScoped<ILoggingService, LoggingService>();
services.AddScoped<IRealTimeService, RealTimeService>();
services.AddScoped<IProductionStatisticsService, ProductionStatisticsService>();
services.AddScoped<IRulesService, RulesService>();
// 注册配置服务
services.AddScoped<ISystemService, SystemService>();
services.AddScoped<ISystemConfigService, SystemConfigService>();
// 注册中间件服务
services.AddScoped<IDeviceStateMachine, DeviceStateMachine>();
// 注册后台任务服务
services.AddHostedService<BackgroundTaskService>();
// 注册仓储 (仅注册存在的)
services.AddScoped<IDeviceRepository, DeviceRepository>();
services.AddScoped<ITemplateRepository, TemplateRepository>();
services.AddScoped<IProductionRepository, ProductionRepository>();
services.AddScoped<IProgramProductionSummaryRepository, ProgramProductionSummaryRepository>();
services.AddScoped<IProductionSummaryRepository, ProductionSummaryRepository>();
services.AddScoped<IAlarmRepository, AlarmRepository>();
services.AddScoped<ICollectionTaskRepository, CollectionTaskRepository>();
services.AddScoped<ICollectionResultRepository, CollectionResultRepository>();
services.AddScoped<ICollectionLogRepository, CollectionLogRepository>();
services.AddScoped<ILogRepository, LogRepository>();
services.AddScoped<IUserRepository, UserRepository>();
// 注册Ping服务
services.AddScoped<IPingService, PingService>();
// 注册数据解析和存储服务
services.AddScoped<IDataParserService, DataParserService>();
services.AddScoped<IDataStorageService, DataStorageService>();
services.AddScoped<IRetryService, RetryService>();
services.AddScoped<IRetryService, RetryService>();
// 注册SignalR Hub
services.AddSingleton<RealTimeHub>();
}
}
}