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 ...
随机推荐
- YII配置mysql读写分离
Mysql 读写分离 YIi 配置 <?php return [ 'class' => 'yii\db\Connection', 'masterConfig' => [ // 'ds ...
- 通过代理上网时,qq等应用程序连网出错
虽然现在基本上都用无线,有线宽带等,但是有时候还是避免不了通过代理上网时,于是就发生浏览器可以正常浏览网页,qq等应用程序连接出错等问题,上网搜了好长时间, 都没解决问题,后来慢慢琢磨(其实是乱 ...
- 基于内存,redis,mysql的高速游戏数据服务器设计架构 ZT
zt http://www.cnblogs.com/captainl1993/p/4788236.html 1.数据服务器详细设计 数据服务器在设计上采用三个层次的数据同步,实现玩家数据的高速获取和 ...
- java 内存, 类加载g
1. java 内存区域 方法区 虚拟机栈 本地方法栈 堆 程序计数器 其中 : 方法区 和 堆 是所有线程共享的 , 其他是线程隔离的 1. 程序计数器 : 可以看做是当前线程所执行的字节码的 ...
- rbtposeekf的注意事项
1.发布的odom topic以及 imu topic必须加上协方差部分:2.在发布odom的时候,去掉里面的odom->base_link的tf,因为这个tf会在robot_pose_ekf包 ...
- EasyUI 分页 简洁代码
做分页代码,看到网上很多人实现的方法,那是各种调用,各种获取对象.我很不解,因为Easyui已经给我们了分页的具体实现,为什么有些人要画蛇添足呢. 其实真正的分页,在你的代码中,别人可能都没有注意到, ...
- PHP代码不应有的坏习惯
>>使用echo取代print >>使用str_replace取代preg_replace, 除非你绝对需要 >>不要使用 short tag >>简单 ...
- SpringBoot 多环境配置
转载:https://www.cnblogs.com/gdpuzxs/p/7191436.html 在我们的实际开发中,一般都有三套环境,开发环境,测试环境,生产环境,三套环境的数据库连接配置也有所不 ...
- 【Web】CSS实现绝对定位元素水平垂直居中
网页中常常需用让绝对定位元素水平垂直居中,下面介绍2种方法: 一 元素宽度未知 <!DOCTYPE html> <html lang="en"> <h ...
- [C#]this.Invoke和this.BeginInvoke的区别
private void button1_Click(object sender, EventArgs e) { "; this.Invoke(new EventHandler(delega ...