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 定义

WebApi开发框架V3.0 (.NETCore+EFCore) 增加AppSettings全局参数类

 

使用方法

 

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;
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
C/S框架网
下一篇:没有了
评论列表

发表评论

评论内容
昵称:
关联文章

WebApi开发框架V3.0 (.NETCore+EFCore) 增加AppSettings全局参数
CSFramework.WebApi V3.0 WebApi开发框架(.NETCore+EFCore)
CSFramework.WebApi V3.0 WebApi开发框架(.NETCore+EFCore)
.NETCore WebApi开发框架 appsettings.json 配置详解
.NETCore WebApi框架:从appsettings.json 读取参数配置
WebApi开发框架V3.0 - Api_User表增加客户标识
CSFramework.WebApi开发框架V3.0 (.NETCore+EFCore) - 客户交接文件清单
C/S软件授权注册系统V3.0(Winform+WebApi+.NET8+EFCore版本)
WebApi开发框架增加Timestamp参数提升系统安全级别
CSFrameworkV6.1旗舰版 - appsettings.json 配置文件增加参数
WebApi开发框架V3.0 - Hello World 接口实现
.NETCore WebApi后端开发框架+达梦数据库测试报告
WebApi NETCore框架 - APIProviderFactory 调用WebApi接口
服务端增加WCF服务全局异常处理机制
ASPNET WebAPI后端框架全局异常跟踪及处理机制,保存系统异常消息日志
《CS框架V3.0开发环境配置和部署》
CSFramework.WebApiV3.UserContext详解
CSFramework.WebApiV3.UserContext详解
ASP.NETCore的WebApplication和WebApplicationBuilder介绍
C# C/S系统开发框架高级版V3.0正式发布!