WebApi开发框架V3.0 (.NETCore+EFCore) 增加AppSettings全局参数类
AppSettings全局参数类
定义AppSettings全局参数类,启动WebApi服务时配置及读取appsettings.json参数到该类,方便随时读取系统参数。
C# 全选
/// <summary>
/// 启动WebApi加载appsettings.json参数
/// </summary>
public static class AppSettings
{
/// <summary>
/// 当前应用程序配置
/// </summary>
public static IConfiguration Configuration { get; private set; }
public static CacheOptions CacheOptions { get; private set; }
public static JwtOptions JwtOptions { get; private set; }
public static DataSignKeys DataSignKeys { get; private set; }
public static ThrottleOptions ThrottleOptions { get; private set; }
public static WebApiOptions WebApiOptions { get; private set; }
public static DatabaseOptions WebApiSystemDb { get; private set; }
public static DatabaseOptions WebApiLogDb { get; private set; }
public static DatabaseOptions CSV6_SystemDb { get; private set; }
public static DatabaseOptions CSV6_NormalDb { get; private set; }
public static void Init(IServiceCollection services, IConfiguration configuration)
{
Configuration = configuration;
//加载根参数
ClientId = configuration["ExpMinutes"].ToStringEx();
UseCSFrameworkDb = configuration["UseCSFrameworkDb"].ToBoolean();
SyncCSFrameworkUser = configuration["SyncCSFrameworkUser"].ToBoolean();
UseSwagger = configuration["UseSwagger"].ToBoolean();
WorkderId = configuration["WorkderId"].ToInt();
services.Configure<CacheOptions>(configuration.GetSection("Cache"));
services.Configure<JwtOptions>(configuration.GetSection("jwt"));
services.Configure<DataSignKeys>(configuration.GetSection("DataSignKeyOptions"));
services.Configure<ThrottleOptions>(configuration.GetSection("ThrottleOptions"));
services.Configure<WebApiOptions>(configuration.GetSection("WebApiOptions"));
//WebApi框架数据库
services.Configure<DatabaseOptions>(configuration.GetSection("Database:WebApiSystemDb"));
services.Configure<DatabaseOptions>(configuration.GetSection("Database:WebApiLogDb"));
//CSFramework C/S框架数据库
services.Configure<DatabaseOptions>(configuration.GetSection("Database:SystemDb"));
services.Configure<DatabaseOptions>(configuration.GetSection("Database:NormalDb"));
CacheOptions = configuration.GetSection("Cache").Get<CacheOptions>();
JwtOptions = configuration.GetSection("jwt").Get<JwtOptions>();
DataSignKeys = configuration.GetSection("DataSignKeyOptions").Get<DataSignKeys>();
ThrottleOptions = configuration.GetSection("ThrottleOptions").Get<ThrottleOptions>();
WebApiOptions = configuration.GetSection("WebApiOptions").Get<WebApiOptions>();
WebApiSystemDb = configuration.GetSection("Database:WebApiSystemDb").Get<DatabaseOptions>();
WebApiLogDb = configuration.GetSection("Database:WebApiLogDb").Get<DatabaseOptions>();
CSV6_SystemDb = configuration.GetSection("Database:SystemDb").Get<DatabaseOptions>();
CSV6_NormalDb = configuration.GetSection("Database:NormalDb").Get<DatabaseOptions>();
}
/// <summary>
/// 机器码序号, 用于雪花id生成器
/// </summary>
public static int WorkderId { get; set; }
/// <summary>
/// 客户标识,如:CSFramework.WebApi
/// </summary>
public static string ClientId { get; set; }
/// <summary>
/// 使用CSFramework框架的数据库(CSFrameworkV6_System,CSFrameworkV6_Normal)
/// </summary>
public static bool UseCSFrameworkDb { get; set; }
/// <summary>
/// 同步tb_MyUser表的用户到WebApi.Api_User表
/// </summary>
public static bool SyncCSFrameworkUser { get; set; }
/// <summary>
/// 使用swagger
/// </summary>
public static bool UseSwagger { get; set; }
}
appsettings.json 定义
使用方法
C# 全选
var builder = Host.CreateDefaultBuilder(args);
builder.UseAppSettings();//读取系统全局参数
C# 全选
/// <summary>
/// 读取系统全局参数
/// </summary>
/// <param name="hostBuilder"></param>
/// <returns></returns>
public static IHostBuilder UseAppSettings(this IHostBuilder hostBuilder)
{
hostBuilder.ConfigureServices((builder, services) =>
{
Console.WriteLine("Load AppSettings.json...");
AppSettings.Init(services, builder.Configuration);
});
return hostBuilder;
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
WebApi-NETCore-V3 C/S框架网