.NET Core 控制台程序没有 ASP.NET Core 的 IWebHostBuilder 与 Startup.cs ,那要读 appsettings.json、注依赖、配日志、设 IOptions 该怎么办呢?因为这些操作与 ASP.NET Core 无依赖,所以可以自己动手,轻松搞定。

1、读 appsettings.json ,ConfigurationBuilder 上

varconf = newConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", true, true) .AddJsonFile("appsettings.Development.json", true, true) .Build();

需要安装 nuget 包 Microsoft.Extensions.Configuration 、Microsoft.Extensions.Configuration.FileExtensions 、Microsoft.Extensions.Configuration.Json

2、注依赖,IServiceCollection + IServiceProvider 一起来

IServiceCollection services = newServiceCollection();//...services.AddSingleton<CosClient> ();IServiceProvider serviceProvider = services.BuildServiceProvider();varcosClient = serviceProvider.GetService<CosClient>();

需要安装 nuget 包 Microsoft.Extensions.DependencyInjection

3、配日志, AddLogging 与 ILoggingBuilder 肩并肩

services.AddLogging(builder => builder .AddConfiguration(conf.GetSection("Logging")) .AddConsole());

需要安装 nuget 包 Microsoft.Extensions.Logging 、Microsoft.Extensions.Logging.Configuration 、Microsoft.Extensions.Logging.Console

4、设IOptions,AddOptions() 与 Configure<T> 齐步走

services.AddOptions();services.Configure<CosClientOptions>(conf.GetSection( "cosClient"));

需要安装 nuget 包 Microsoft.Extensions.Options 与 Microsoft.Extensions.Options.ConfigurationExtensions

完整代码:

classProgram{

staticasyncTask Main( string[] args) { varconf = newConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", true, true) .AddJsonFile("appsettings.Development.json", true, true) .Build(); IServiceCollection services = newServiceCollection(); services.AddLogging(builder => builder .AddConfiguration(conf.GetSection("Logging")) .AddConsole()); services.AddOptions(); services.Configure<CosClientOptions>(conf.GetSection( "cosClient")); services.AddSingleton<CosClient> (); IServiceProvider serviceProvider = services.BuildServiceProvider();

varcosClient = serviceProvider.GetService<CosClient> (); }}

NET Core 控制台程序读 appsettings.json 、注依赖、配日志、设 IOptions的更多相关文章

  1. 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful) .net core 控制台程序使用依赖注入(Autofac)

    大比速:remoting.WCF(http).WCF(tcp).WCF(RESTful).asp.net core(RESTful) 近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只 ...

  2. 【ASP.NET Core分布式项目实战】(五)Docker制作dotnet core控制台程序镜像

    Docker制作dotnet core控制台程序镜像 基于dotnet SDK 新建控制台程序 mkdir /home/console cd /home/console dotnet new cons ...

  3. 在.NET Core控制台程序中使用依赖注入

    之前都是在ASP.NET Core中使用依赖注入(Dependency Injection),昨天遇到一个场景需要在.NET Core控制台程序中使用依赖注入,由于对.NET Core中的依赖注入机制 ...

  4. .Net Core 控制台程序错误:Can not find runtime target for framework '.NETCoreApp,Version=v1.0' compatible with one of the target runtimes: 'win10-x64, win81-x64, win8-x64, win7-x64'.

    .Net Core 控制台程序错误:Can not find runtime target for framework '.NETCoreApp,Version=v1.0' compatible wi ...

  5. 如何在.NET Core控制台程序中使用依赖注入

    背景介绍 依赖注入(Dependency Injection), 是面向对象编程中的一种设计原则,可以用来减低代码之间的耦合度.在.NET Core MVC中 我们可以在Startup.cs文件的Co ...

  6. VisualStudioCode创建的asp.net core控制台程序部署到linux

    1.asp.net core控制台程序 static void Main(string[] args) { ; ) { Console.WriteLine("Hello World!&quo ...

  7. Net Core 控制台程序使用Nlog 输出到log文件

    using CoreImportDataApp.Common; using Microsoft.Extensions.Configuration; using Microsoft.Extensions ...

  8. Mac/Windows开发跨平台.NET Core 控制台程序

    自从微软开始在Github上开源搞.NET Core后,.NET的跨平台逐渐就成真了.多年使用各种语言,说实话还是csharp用起来最舒服.不过现在的工作环境里使用它的机会比较少,大部分时候只是用来写 ...

  9. Supervisor守护DotNet Core控制台程序

    Supervisor 相信对Linux系统很熟的都知道这个软件,基于Python写的一个守护进程软件.具体的介绍和使用我就不再赘述了. 使用asp.net core 部署在Linux常用的方法 我们可 ...

随机推荐

  1. Windows server2008R2 企业内部搭建虚拟专用网络服务

    VPN英文全称是“Virtual Private Network”,就是“虚拟专用网络”.可以远程帮助用户.分公司.商业伙伴及供应商同公司的内部网建立可信的安全连接,用于经济有效地连接到商业伙伴和用户 ...

  2. SpringBoot+mybatis使用@Transactional无效

    项目中新增过程中如果出现异常需要回滚, 在service实现方法中使用@Transactional注解失效 解决: 1, 在controller中使用try{}catch捕捉异常 2, 在servic ...

  3. AI 生成式对抗网络(GAN)

    生成式对抗网络(Generative Adversarial Network,简称GAN),主要由两部分构成:生成模型G和判别模型D.训练GAN就是两种模型的对抗过程. 生成模型:利用任意噪音(ran ...

  4. Parallel 类并行任务(仅仅当执行耗时操作时,才有必要使用)

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  5. 洛谷 P5020 货币系统

    题目描述 在网友的国度中共有$ n $种不同面额的货币,第 i种货币的面额为 \(a[i]\),你可以假设每一种货币都有无穷多张.为了方便,我们把货币种数为\(n\).面额数组为 \(a[1..n]\ ...

  6. Python学习--Python运算符

    什么是运算符? 举个简单的例子 4 + 5 = 9 . 例子中,4 和 5 被称为操作数,"+" 称为运算符. Python语言支持以下类型的运算符: 算数运算符 比较(关系)运算 ...

  7. 使用Flame Graph进行系统性能分析

    关键词:Flame Graph.perf.perl. FlameGraph是由BrendanGregg开发的一款开源可视化性能分析工具,形象的成为火焰图. 从底向上像火苗一样逐渐变小,也反映了相互之间 ...

  8. linux查询日志常用命令,经常更新

    1.grep命令 grep -c "查询内容" filename    ------c,是小写,可以知道你要查询的内容在这个文件中是否存在 grep -C 10 "查询内 ...

  9. L2-4 部落 (25 分)

    在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不同的朋友圈.我们认为朋友的朋友都算在一个部落里,于是要请你统计一下,在一个给定社区中,到底有多少个互不相交的部落?并且检查任意两个人是否属于同 ...

  10. CF452F Permutations/Luogu2757 等差子序列 树状数组、Hash

    传送门--Luogu 传送门--Codeforces 如果存在长度\(>3\)的等差子序列,那么一定存在长度\(=3\)的等差子序列,所以我们只需要找长度为\(3\)的等差子序列.可以枚举等差子 ...