- 为全部 5 个项目(Avalonia-API、Avalonia-Common、Avalonia-EFCore、 Avalonia-PC、Avalonia-Services)中缺失注释的类、方法、属性、字段、 接口成员等补全中文 XML 文档注释 - 共修改约 37 个文件,补全约 220+ 处注释 - 修复 ServiceEndpointCollection.cs 中 MapDelete<TService> 语法错误 - 修复 PcAuthService.cs 中 const prefix 位置错乱导致编译失败的问题 - 扫描结果:缺失项 0 - 构建结果:4/4 项目编译通过
76 lines
3.2 KiB
C#
76 lines
3.2 KiB
C#
using Avalonia_API.Authentication;
|
||
using Avalonia_EFCore.Database;
|
||
using Avalonia_Services.Core;
|
||
using Avalonia_Services.Endpoints;
|
||
using Avalonia_Services.Services;
|
||
using Avalonia_Services.Services.AuthService;
|
||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||
using Microsoft.IdentityModel.Tokens;
|
||
using System.Text;
|
||
|
||
namespace Avalonia_API.Configuration
|
||
{
|
||
/// <summary>
|
||
/// API 项目服务配置扩展类,负责注册数据库、鉴权、业务服务和统一端点。
|
||
/// </summary>
|
||
public static class ServicesConfiguration
|
||
{
|
||
/// <summary>
|
||
/// 注册统一端点及其依赖的服务(含数据库)。
|
||
/// 所有业务端点定义在 Avalonia-Services/Endpoints/AppEndpoints.cs。
|
||
/// </summary>
|
||
public static IServiceCollection AddUnifiedApiServices(this IServiceCollection services, IConfiguration configuration)
|
||
{
|
||
// ---- 数据库 ----
|
||
// 从 appsettings.json 读取 DatabaseConfiguration 节
|
||
// 注册默认数据库提供程序(SQLite / MySQL / PostgreSQL / SqlServer)
|
||
DatabaseProviderRegistry.RegisterDefaults();
|
||
|
||
var databaseConfig = configuration
|
||
.GetSection(nameof(DatabaseConfiguration))
|
||
.Get<DatabaseConfiguration>()
|
||
?? DatabaseConfiguration.ForSQLite("app.db");
|
||
|
||
// 注册 AppDataContext(共享数据上下文)
|
||
services.AddAppDatabase<AppDataContext>(databaseConfig);
|
||
|
||
// ---- 业务服务 ----
|
||
services.AddScoped<WeatherForecastService>();
|
||
|
||
// ---- API 鉴权 ----
|
||
var jwtSection = configuration.GetSection("Jwt");
|
||
services.Configure<JwtOptions>(jwtSection);
|
||
var jwtOptions = jwtSection.Get<JwtOptions>() ?? new JwtOptions();
|
||
services
|
||
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||
.AddJwtBearer(options =>
|
||
{
|
||
options.TokenValidationParameters = new TokenValidationParameters
|
||
{
|
||
ValidateIssuer = true,
|
||
ValidateAudience = true,
|
||
ValidateLifetime = true,
|
||
ValidateIssuerSigningKey = true,
|
||
ValidIssuer = jwtOptions.Issuer,
|
||
ValidAudience = jwtOptions.Audience,
|
||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.SigningKey)),
|
||
ClockSkew = TimeSpan.FromMinutes(1),
|
||
};
|
||
});
|
||
services.AddAuthorization();
|
||
services.AddScoped<JwtTokenService>();
|
||
services.AddScoped<RefreshTokenService>();
|
||
services.AddScoped<IApiAuthEndpointService, ApiAuthEndpointService>();
|
||
|
||
// ---- 统一端点 ----
|
||
var endpointBuilder = new ServiceEndpointBuilder();
|
||
AppEndpoints.Configure(endpointBuilder);
|
||
AuthEndpoints.ConfigureApi(endpointBuilder);
|
||
var endpoints = endpointBuilder.Build();
|
||
services.AddSingleton(endpoints);
|
||
|
||
return services;
|
||
}
|
||
}
|
||
}
|