假設Node和npm已經安裝 npm install -g @servicestack/cli 執行命令dotnet-new selfhost SSHost 這樣就創建了ServiceStack的控制台程式,用VS2017解決方案,添加如下代碼 項目SSHost里添加配置文件appsettings. ...
假設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(0); }); } } 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", 200); Console.WriteLine($"MaxRecords: {AppSettings.GetString("MaxRecords")}"); pageConfig.LightListPageSize = 50; pageConfig.GatewayListPageSize = 60; //設置屬性,然後讀取對象 AppSettings.Set<int>("PageConfig:LightListPageSize", 50); var pageConfig2 = AppSettings.Get<PageConfig>("PageConfig"); Console.WriteLine("設置配置完畢"); #endregion } } }
項目SSHost里添加配置文件appsettings.Json,裡面配置內容如下
{ "MaxRecords": "300", "PageConfig": { "LightListPageSize": "10", "GatewayListPageSize": "20" }, "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true" } }
編譯運行,出現如下錯誤信息
1>------ 已啟動全部重新生成: 項目: SSHost.ServiceModel, 配置: Debug Any CPU ------ 1>SSHost.ServiceModel -> D:\SSHost\SSHost.ServiceModel\bin\Debug\netstandard2.0\SSHost.ServiceModel.dll 2>------ 已啟動全部重新生成: 項目: SSHost.ServiceInterface, 配置: Debug Any CPU ------ 2>SSHost.ServiceInterface -> D:\SSHost\SSHost.ServiceInterface\bin\Debug\netstandard2.0\SSHost.ServiceInterface.dll 3>------ 已啟動全部重新生成: 項目: SSHost, 配置: Debug Any CPU ------ 4>------ 已啟動全部重新生成: 項目: SSHost.Tests, 配置: Debug Any CPU ------ 4>SSHost.Tests -> D:\SSHost\SSHost.Tests\bin\Debug\netcoreapp2.1\SSHost.Tests.dll 3>Program.cs(47,20,47,31): error CS1061: “IConfigurationBuilder”未包含“AddJsonFile”的定義,並且找不到可接受第一個“IConfigurationBuilder”類型參數的可訪問擴展方法“AddJsonFile”(是否缺少 using 指令或程式集引用?) 3>已完成生成項目“SSHost.csproj”的操作 - 失敗。 ========== 全部重新生成: 成功 3 個,失敗 1 個,跳過 0 個 ==========
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