.netcore 的一些简单封装配置文件配置动态注入到ioc

  • 场景一些配置需要经常用到可以尝试把配置配置到配置类中需要使用时只需要通过构造函数注入配置类即可。
  • json文件结构示例
     "SmsSetting": {
        //253
        "Host": "123",
        "Account": "123",
        "Password": "123",
        "Sign": "123",//标签
        //梦网   
        "MonYunHost": "123",
        "Userid": "123",
        "Pwd": "123",
        //华为
        "HuaWeiHost": "123",
        "AppKey": "123",
        "AppSecret": "123",//app密钥
        "TemplateId": "123",//模板Id
        "Sender": "123",//短信通道
        "StatusCallBack": ""
      },

     

  • 配置类示例
     public class SmsSettingConfigModel : IConfigModel
        {
            #region 253 创蓝
            public string Host { get; set; }
    
            public string Account { get; set; }
    
            public string Password { get; set; }
            /// <summary>
            /// 签名
            /// </summary>
            public string Sign { get; set; }
            #endregion
    
            #region 梦网
            /// <summary>
            /// 梦网的地址
            /// </summary>
            public string MonYunHost { get; set; }
            /// <summary>
            /// 长度最大6个字符,统一大写,如提交参数中包含apikey,则可以不用填写该参数及pwd,两种鉴权方式中只能选择一种方式来进行鉴权
            /// </summary>
            public string Userid { get; set; }
            /// <summary>
            /// 定长小写32位字符,如提交参数中包含apikey,则可以不用填写该参数及userid,两种鉴权方式中只能选择一种方式来进行鉴权。密码规则详见
            /// </summary>
            public string Pwd { get; set; }
            /// <summary>
            /// 32位长度,由梦网提供,与userid及pwd一样用于鉴权,如提交参数中包含userid及pwd,则可以不用填写该参数
            /// </summary>
            //public string Apikey { get; set; }
            #endregion
    
            #region 华为
            /// <summary>
            /// 华为地址
            /// </summary>
            public string HuaWeiHost { get; set; }
            /// <summary>
            /// APP_Key
            /// </summary>
            public string AppKey { get; set; }
            /// <summary>
            /// AppSecret
            /// </summary>
            public string AppSecret { get; set; }
            /// <summary>
            /// 通道号
            /// </summary>
            public string Sender { get; set; }
            /// <summary>
            /// 模板ID
            /// </summary>
            public string TemplateId { get; set; }
            /// <summary>
            /// 选填,短信状态报告接收地址,推荐使用域名,为空或者不填表示不接收状态报告
            /// </summary>
            public string StatusCallBack { get; set; }
            #endregion
        }
      public interface IConfigModel
        {
        }

     

  • 注入核心代码
       /// <summary>
            /// 添加configuration到配置类
            /// </summary>
            /// <param name="services"></param>
            private static void AddConfiguration(this IServiceCollection services)
            {
                var types = AppDomain.CurrentDomain.GetAssemblies()
                    .SelectMany(oo => oo.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IConfigModel))))
                    .ToArray();
    
                foreach (var type in types)
                {
                    services.AddSingleton(type, provider =>
                    {
                        var sectionName = type.Name.Replace("ConfigModel", "");//截取相关代码配置类截取次部分和配置根节点匹配
                        var config = provider.GetService<IConfiguration>();
                        var model = config.GetSection(sectionName).Get(type);
                        return model;
                    });
                }
            }

     

  • 最终startup 进行注入     services.AddConfiguration();
  • 实际使用通过构造函数注入SmsSettingConfigModel中的所有熟悉都有值了 
        SmsSettingConfigModel _smsSetting;
            IHttpClientFactory _httpClientFactory;
            public ChuangLanSendMessage(
                SmsSettingConfigModel smsSetting,
                IHttpClientFactory httpClientFactory)
            {
                _smsSetting = smsSetting;
                _httpClientFactory = httpClientFactory;
            }

     

posted @ 2020-01-19 11:36  雨V幕  阅读(1040)  评论(0编辑  收藏  举报