none
.NET Core 如何在其它类库读取 appsettings.json 或者其它配置文件? RRS feed

答案

  • 你好,

    下面的文章提供了3步 来访问appsettings.json 里面的内容。

    #Json

    {
      "ServiceSettings": {
        "NewsMainUrl": "https://newsapi.org",
        "NewsApiKey": "abc"
      },
    
      "BALSettings": {
        "Source": "xyz",
        "FilterTerms": "abc;def;"
      }
    }

    1.  建一个model class 来映射appsettings.json里面的字段

    namespace Tweet.Entities
    {
        public class BALSettings
        {
            public string Source { get; set; }
            public string FilterTerms { get; set; }
        }
    
       public class ServiceSettings
        {
            public string NewsMainUrl { get; set; }
            public string NewsApiKey { get; set; }
        }
    
    }

    2. 通过DI容器注册 appsettings.json 到相关的 model classes 里面。

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
       services.Configure<BALSettings>(Configuration.GetSection("BALSettings"));
       services.Configure<ServiceSettings>(Configuration.GetSection("ServiceSettings"));
    }

    3. 在类库项目中访问 appsettings.json section.

    注意: Microsoft.Extensions.Options 要通过Nuget 来安装。

    PM> Install-Package Microsoft.Extensions.Options 

    using Microsoft.Extensions.Options;
    
    public class NewsService : INewsService
        {
            private readonly IOptions<ServiceSettings> _serviceSettings;
    
            public NewsService(IOptions<ServiceSettings> serviceSettings)
            {
                _serviceSettings = serviceSettings;
            }
    
            public string composeUrl()
            {
                return _serviceSettings.Value.NewsMainUrl + "&apiKey=" + _serviceSettings.Value.NewsApiKey;
            }
        }

    https://hassantariqblog.wordpress.com/2017/02/20/asp-net-core-step-by-step-guide-to-access-appsettings-json-in-web-project-and-class-library/

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2018年9月3日 5:22
    版主

全部回复

  • 1.在其他类的构造函数进行构造
    public RedisClient(IConfiguration configuration)
        {
            _connectionString = configuration.GetSection("redis")["ConnectionString"];
        }
    }
    {
      "redis": {
        "ConnectionString": "xxx"
      }
    }
    appsettings.json的配置如上

    2018年9月3日 3:35
  • 你好,

    下面的文章提供了3步 来访问appsettings.json 里面的内容。

    #Json

    {
      "ServiceSettings": {
        "NewsMainUrl": "https://newsapi.org",
        "NewsApiKey": "abc"
      },
    
      "BALSettings": {
        "Source": "xyz",
        "FilterTerms": "abc;def;"
      }
    }

    1.  建一个model class 来映射appsettings.json里面的字段

    namespace Tweet.Entities
    {
        public class BALSettings
        {
            public string Source { get; set; }
            public string FilterTerms { get; set; }
        }
    
       public class ServiceSettings
        {
            public string NewsMainUrl { get; set; }
            public string NewsApiKey { get; set; }
        }
    
    }

    2. 通过DI容器注册 appsettings.json 到相关的 model classes 里面。

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
       services.Configure<BALSettings>(Configuration.GetSection("BALSettings"));
       services.Configure<ServiceSettings>(Configuration.GetSection("ServiceSettings"));
    }

    3. 在类库项目中访问 appsettings.json section.

    注意: Microsoft.Extensions.Options 要通过Nuget 来安装。

    PM> Install-Package Microsoft.Extensions.Options 

    using Microsoft.Extensions.Options;
    
    public class NewsService : INewsService
        {
            private readonly IOptions<ServiceSettings> _serviceSettings;
    
            public NewsService(IOptions<ServiceSettings> serviceSettings)
            {
                _serviceSettings = serviceSettings;
            }
    
            public string composeUrl()
            {
                return _serviceSettings.Value.NewsMainUrl + "&apiKey=" + _serviceSettings.Value.NewsApiKey;
            }
        }

    https://hassantariqblog.wordpress.com/2017/02/20/asp-net-core-step-by-step-guide-to-access-appsettings-json-in-web-project-and-class-library/

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2018年9月3日 5:22
    版主
  • 感谢回复!
    2018年9月3日 14:43