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 ...
随机推荐
- Best Sightseeing Pair LT1014
Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and t ...
- 通过flask中的Response返回json数据
使用flask的过程中,发现有时需要生成一个Response并返回.网上查了查,看了看源码,找到了两种办法: from flask import Response, json Response(jso ...
- json(原生态)
什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言 * JSO ...
- css进阶篇
一.css的属性值 1)字体属性 font-size: 5px; /* 字体大小 */ font-size: 20px/50%/larger /* 字体的大小 */ font-family:'Luci ...
- 关于Rigidbody,Collider和CharacterController三者之间的关系和用法的总结
Rigidbody:多用在“物体”上,因为“物体”都是“死”的,他们的运动一般都是靠物理系统.所以对于Rigidbody的移动,不要用Translate(),要用各种“力”, 比如:Rigidbody ...
- 2018.11.09 洛谷P1110 [ZJOI2007]报表统计(multiset)
传送门 sb题. 直接用两个multisetmultisetmultiset维护相邻两个数的差值和所有数的前驱后继. 插入一个数的时候更新一下就行了. 代码: #include<bits/std ...
- CHAPITRE II
J'ai ainsi vécu seul, sans personne avec qui parler véritablement, jusqu'à une panne[pan][机]故障 dans ...
- mybatis xml中的大于、小于等符号写法
xml特殊符号转义写法 < < > > <> <> & & ' ...
- mysql重装之后 复制data
(哇,编程小白的第一篇博客丫,激动) Q one:mysql需要重装,数据该怎么办. 方法一:数据表最好是导出成.sql文件,这样才比较安全. 方法二:直接copy了data文件:在mysql安装盘下 ...
- 总结一下《vue的使用》
1.用vue创建项目的时候, 1.安装axios,对axios进行处理,创建axios.js文件,设置基础请求地址, 设置前置守卫和独享守卫,对请求数据进行设置,(特别实在进行token验证的时候特别 ...