This is something that strangely doesn’t seem to be that well documented and took me a while to figure out though in the end it’s pretty simple.

All that’s required is to add the following NuGet packages and an appsettings.json file.

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.FileExtensions
  • Microsoft.Extensions.Configuration.Json

The appsettings.json files “Copy to Output Directory” property should also be set to “Copy if newer” so that the application is able to access it when published.

The settings are injected in the main method rather than in the startup method as with web apps but the code is essentially the same.

        static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); IConfigurationRoot configuration = builder.Build(); Console.WriteLine(configuration.GetConnectionString("Storage"));
}

Adding appsettings.json to a .NET Core console app的更多相关文章

  1. 011.Adding Search to an ASP.NET Core MVC app --【给程序添加搜索功能】

    Adding Search to an ASP.NET Core MVC app 给程序添加搜索功能 2017-3-7 7 分钟阅读时长 作者 本文内容 1.Adding Search by genr ...

  2. Talking appsettings.json in Asp.Net Core

    在ASP.NET Core中,默认提供了三个运行时环境变量,通过查看Hosting源代码我们可以看到,分别是Development.Staging.Production public static c ...

  3. Azure Kay Vault(一).NET Core Console App 获取密钥保管库中的机密信息

    一,引言 Azure 密钥保管库用于存储敏感信息,例如链接字符串,密码,API 密钥等.我们无法直接从Azure 密钥库中访问机密!那么我们如何才能访问应用程序中的机密信息?比如,在我们的实际项目中, ...

  4. .net core 读取appsettings.json乱码

    .net core 读取配置文件乱码:vs2019读取appsettings.json乱码问题; .net core 读取appsettings.json乱码问题;用notepad++或者其他编辑器打 ...

  5. .Net Core 实践 - 如何在控制台应用(.Net Core)使用appsettings.json配置

    新建控制台应用(.Net Core)程序 添加json文件,命名为appsettings.json,设置文件属性 如果较新则复制.添加内容如下 { "MyWords" : &quo ...

  6. NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions

    .NET Core 控制台程序没有 ASP.NET Core 的 IWebHostBuilder 与 Startup.cs ,那要读 appsettings.json.注依赖.配日志.设 IOptio ...

  7. ASP .NET CORE 根据环境变量支持多个 appsettings.json

    0.背景 在开发项目的过程当中,生产环境与调试环境的配置肯定是不一样的.拿个最简单的例子来说,比如连接字符串这种东西,调试环境肯定是不能连接生产数据库的.在之前的话,这种情况只能说是你 COPY 两个 ...

  8. 循序渐进学.Net Core Web Api开发系列【6】:配置文件appsettings.json

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.本篇概述 本篇描 ...

  9. ASP.NET Core appsettings.json 文件

    ASP.NET Core appsettings.json 文件 在本节中,我们将讨论 ASP.NET Core 项目中appsettings.json文件的重要性. 在以前的 ASP.NET 版本中 ...

随机推荐

  1. Redis集群伸缩

    集群扩容 前提准备,目前集群中一共有6台机器,端口号分别是6381.6382.6383.6384.6385.6386 1) 准备新节点 准备两个新节点,端口号为6387和6388,配置和以前集群配置一 ...

  2. Docker 容器镜像删除

    1.停止所有的container,这样才能够删除其中的images: docker stop $(docker ps -a -q) 如果想要删除所有container的话再加一个指令: docker ...

  3. openlayers三:添加图片和图标

    openlayers添加图片是指: 添加在地图上的图片会跟随地图同步放大缩小 而添加图标是指: 添加在地图上的图片不会跟随地图同步放大缩小 添加图片: 首先初始化图片图层: initImageLaye ...

  4. .Net Core 学习笔记1——包、元包、框架

    .Net Core 是由NuGet包(package)组成的平台. 一起使用的多个包的集合:元包(Metapackage) package 包 (对应以前的程序集概念) Framework 框架 as ...

  5. MongoDB个人简单总结

    当同一个变量被连续插入两次会出现id相同的异常,但是同样内容的两个变量同时插入不会有问题,可能是同一个变量同一个地址生成id相同,导致异常. 默认登陆不需要用户名密码认证,需要密码认证登陆需要在adm ...

  6. mysql(mariadb)主从配置

    环境: 内核版本:uname -r 系统版本:cat /etc/redhat-release

  7. nexus 10 救砖 安装lineage OS 15 并 root

    因为平板自带的谷歌应用太烦人了,想root之后卸载它们. 一.root nexus 10 官方系统 1.把img拷贝到platform-tools(Android官网下载)文件夹 2.platform ...

  8. Shell企业案例实战和企业面试题

    shell企业面试题 1.批量创建带有随机小写字符文件程序 使用for循环在/pizza目录下创建10个html文件,其中每个文件包含10个随机小写字母加固定字母_pizza 1.思路分析: 核心是: ...

  9. golang 日期时间处理

    package main import ( "fmt" "time" ) func main() { fmt.Println(time.Now()) //显示时 ...

  10. redis 安装时候遇到 jemalloc 问题记录

    https://www.cnblogs.com/lovemdx/p/3199886.html https://blog.csdn.net/yfkiss/article/details/7035579 ...