ServiceStack NetCoreAppSettings 配置文件读取和设置
假设Node和npm已经安装
npm install -g @servicestack/cli
执行命令dotnet-new selfhost SSHost
这样就创建了ServiceStack的控制台程序,用VS2017解决方案,添加如下代码
using Funq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using SSHost.ServiceInterface;
using System;
using System.IO;
using System.Threading.Tasks; namespace SSHost
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseUrls(Environment.GetEnvironmentVariable("ASPNETCORE_URLS") ?? "http://localhost:5000/")
.Build(); host.Run();
}
} public class Startup
{ public IConfiguration Configuration { get; set; }
public Startup(IConfiguration configuration) => Configuration = configuration; // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{ } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//nuget里添加Microsoft.Extensions.Configuration.json,否则编译不认识AddJsonFile
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile($"appsettings.json", optional: true)
.AddEnvironmentVariables(); Configuration = builder.Build(); app.UseServiceStack(new AppHost
{
AppSettings = new NetCoreAppSettings(Configuration)
}); app.Run(context =>
{
context.Response.Redirect("/metadata");
return Task.FromResult();
});
}
} public class AppHost : AppHostBase
{
public AppHost()
: base("SSHost", typeof(MyServices).Assembly) { } public class PageConfig
{
public int LightListPageSize { get; set; } public int GatewayListPageSize { get; set; }
} public override void Configure(Container container)
{
SetConfig(new HostConfig
{
DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false)
}); #region 读取或者设置NetCoreAppSettings //读取单个值
Console.WriteLine($"MaxRecords: {AppSettings.GetString("MaxRecords")}"); //读取对象属性
Console.WriteLine($"PageConfig: {AppSettings.GetString("PageConfig:LightListPageSize")}"); //读取整个对象
var pageConfig = AppSettings.Get<PageConfig>("PageConfig"); Console.WriteLine($"ConnectionString: {AppSettings.GetString("ConnectionStrings:DefaultConnection")}"); //设置每页记录最大数量为200
AppSettings.Set<int>("MaxRecords", );
Console.WriteLine($"MaxRecords: {AppSettings.GetString("MaxRecords")}"); pageConfig.LightListPageSize = ;
pageConfig.GatewayListPageSize = ; //设置属性,然后读取对象
AppSettings.Set<int>("PageConfig:LightListPageSize", );
var pageConfig2 = AppSettings.Get<PageConfig>("PageConfig"); Console.WriteLine("设置配置完毕"); #endregion }
}
}
项目SSHost里添加配置文件appsettings.Json,里面配置内容如下
{
"MaxRecords": "",
"PageConfig": {
"LightListPageSize": "",
"GatewayListPageSize": ""
},
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
编译运行,出现如下错误信息
>------ 已启动全部重新生成: 项目: SSHost.ServiceModel, 配置: Debug Any CPU ------
>SSHost.ServiceModel -> D:\SSHost\SSHost.ServiceModel\bin\Debug\netstandard2.\SSHost.ServiceModel.dll
>------ 已启动全部重新生成: 项目: SSHost.ServiceInterface, 配置: Debug Any CPU ------
>SSHost.ServiceInterface -> D:\SSHost\SSHost.ServiceInterface\bin\Debug\netstandard2.\SSHost.ServiceInterface.dll
>------ 已启动全部重新生成: 项目: SSHost, 配置: Debug Any CPU ------
>------ 已启动全部重新生成: 项目: SSHost.Tests, 配置: Debug Any CPU ------
>SSHost.Tests -> D:\SSHost\SSHost.Tests\bin\Debug\netcoreapp2.\SSHost.Tests.dll
>Program.cs(,,,): error CS1061: “IConfigurationBuilder”未包含“AddJsonFile”的定义,并且找不到可接受第一个“IConfigurationBuilder”类型参数的可访问扩展方法“AddJsonFile”(是否缺少 using 指令或程序集引用?)
>已完成生成项目“SSHost.csproj”的操作 - 失败。
========== 全部重新生成: 成功 个,失败 个,跳过 个 ==========
nuget里添加Microsoft.Extensions.Configuration.json,否则编译不认识AddJsonFile
再次编译运行
总结一下,ServiceStack的AppSettings功能非常强大,并且非常好用,不仅支持过去的Web.config,也支持.Net Core的appsettings.json,还支持文本文件
想了解更多的情况,可以查看文档:https://github.com/ServiceStack/docs/blob/master/docs/_documentation/AppSettings.md
ServiceStack NetCoreAppSettings 配置文件读取和设置的更多相关文章
- [spring源码学习]二、IOC源码——配置文件读取
一.环境准备 对于学习源码来讲,拿到一大堆的代码,脑袋里肯定是嗡嗡的,所以从代码实例进行跟踪调试未尝不是一种好的办法,此处,我们准备了一个小例子: package com.zjl; public cl ...
- VS2012中,C# 配置文件读取 + C#多个工程共享共有变量 + 整理using语句
(一) C# 配置文件读取 C#工程可以自动生成配置文件,以便整个工程可以使用设置的配置进行后续的处理工作. 1. 首先,右键工程文件-->Properties -->settings-- ...
- C# 配置文件读取与修改(转)
C# 配置文件读取与修改 配置文件在很多情况下都使用到, 配置文件分为两种 一种是应用程序的配置文件, 一种是web的配置文件. 两种配置文件最大的区别是web的配置文件更新之后会实时更新, 应用 ...
- smarty 从配置文件读取变量
smarty变量分3种: Variables [变量] Variables assigned from PHP [从PHP分配的变量] Variables loaded from config fil ...
- 【Spring源码分析】配置文件读取流程
前言 Spring配置文件读取流程本来是和http://www.cnblogs.com/xrq730/p/6285358.html一文放在一起的,这两天在看Spring自定义标签的时候,感觉对Spri ...
- MySql5.7配置文件my.cnf设置
# MySql5.7配置文件my.cnf设置[client]port = 3306socket = /tmp/mysql.sock [mysqld]########################## ...
- Python模块之: ConfigParser 配置文件读取
Python模块之: ConfigParser 配置文件读取 ConfigParser用于读写类似INI文件的配置文件,配置文件的内容可组织为组,还支持多个选项值(option-value)类型. ...
- MySql5.7配置文件my.ini 设置 my.ini文件路径
mysql配置文件my-default.ini my.ini修改后重启无效,原来是路径错了,记录一下: windows操作系统下: 1. 由于我们使用MySql 时,需要修改mysql 的 my.i ...
- MySql5.7 配置文件 my.cnf 设置
https://blog.csdn.net/gzt19881123/article/details/52594783 # MySql5.7配置文件my.cnf设置 [client] port = 33 ...
随机推荐
- json数据映射填充到html元素显示
映射算法做了改进,支持name重复映射 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...
- Redis学习笔记:windows上redis的安装运行
Redis的windows版本地址https://github.com/MicrosoftArchive/redis 下载之后解压之 在当前解压目录下可以看到如下文件 在当前目录下打开命令行窗口,输入 ...
- Spring Boot学习笔记:项目开发中规范总结
Spring Boot在企业开发中使用的很广泛,不同的企业有不同的开发规范和标准.但是有些标准都是一致的. 项目包结构 以下是一个项目常见的包结构 以上是一个项目的基本目录结构,不同的项目结构会有差异 ...
- 用python实现数学多元数学方程式计算
题目:公鸡5元钱一只,母鸡3元钱一只,小鸡3只一块钱,其中公鸡,母鸡,小鸡都必须有,问公鸡,母鸡,小鸡各买多少只刚好凑足100元钱? 一:数学算术分析: x+y+z=100 5x+3y+z/3=100 ...
- tms web core 与 kbmmw 第一次亲密接触
最近,tms 经过1年多,集合了数十名高手大牛,开发出了一个跨时代的产品,就是tms web core. 具体的介绍详见官网,https://www.tmssoftware.com/site/tmsw ...
- python里的字典和集合
一.字典 1.字典的定义 字典是不可变的,是用hash值来存储的.字典的key必须是不可变的(可哈希) dict = {key1:value1 , key2:value2} 2.字典的增删改查 增 直 ...
- 如何将frm文件导入MySql数据库
只要在mysql的安装文件中找到data文件夹,然后在里面建立一个文件夹,比如test.这个test其实就对应着数据库的名称,所以,你想要起什么样的数据库名称就把文件夹起什么名字. 然后把.frm文件 ...
- latex字体
强调 方式:声明:\em 或者 命令\emph,后者是latex2e的命令 区别:声明与命令的作用范围不同:\em改变当前字体直到被其他相应的声明取消(也可以是\em本身),或者当前的环境结束为止,当 ...
- 第06章:MongoDB-CRUD操作--集合
①显示现有的集合 命令:show collections 或者show tables; ②创建集合 隐示 在MongoDB中,你不需要创建集合.当你插入一些文档时,MongoDB 会自动创建集合. d ...
- Struts2-result
所有result-type <result-types> <result-type name="chain" class="com.opensympho ...