ABP 讀取配置文件


原文:
https://www.cnblogs.com/lishidefengchen/p/10688312.html

需求

以前分部分項是從數據庫里面查出來的,現在改為調用另一個項目的接口
趕時間就盡量微調,速度為主

百度找到了一篇文章,原以為可以白嫖的,結果我用的位置不一樣,我是要在Application層讀取配置,不是在web層讀

主要參考的這里

1、定義接口和初始化

        private readonly IRepository<Project, int> _entityRepository;
        private readonly IHostingEnvironment _env;
        private readonly IConfigurationRoot _appConfiguration;

        /// <summary>
        /// 構造函數 
        ///</summary>
        public ProjectAppService(
        IRepository<Project, int> entityRepository
            , IHostingEnvironment env
        )
        {
            _entityRepository = entityRepository;
            _env = env;
            _appConfiguration = GetAppConfiguration(env.ContentRootPath);
        }

        private IConfigurationRoot GetAppConfiguration(string path)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(path)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            builder = builder.AddEnvironmentVariables();

            return builder.Build();
        }

2、獲取配置項里面的url地址

        //獲取分部分項樹2 for jstree by gxy 2020-2-28 16:08:13
        [Abp.Web.Models.DontWrapResult]
        public List<JsTreeResponseOutput2> GetProjectTrees2(string id)
        {
            //// 訪問本地數據庫
            //if (string.IsNullOrWhiteSpace(id) || (id == "#"))
            //{
            //    return GetTree();
            //    //return GetThreeRoot();
            //}
            //else
            //{
            //    return GetAllChildren(int.Parse(id));
            //}


            // 訪問遠程數據庫-蘭州中通道質量管理系統
            using (var httpClient = new HttpClient())
            {
                //var requestUri = "http://localhost:58059/SystemManage/Project/GetProjectsForBIMMP?id=" + id;
                var requestUri = _appConfiguration["SubSystem:ProjectUrl"] + "?id=" + id;

                var httpResponseMessage = httpClient.GetAsync(requestUri).Result.Content.ReadAsStringAsync().Result;
                var list = JsonConvert.DeserializeObject<List<JsTreeResponseOutput2>>(httpResponseMessage);
                return list;
            }
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM