设为首页 加入收藏

TOP

ServiceStack NetCoreAppSettings 配置文件读取和设置(一)
2019-09-17 18:24:53 】 浏览:28
Tags: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(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

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇.NET Core开源快速开发框架Colder.. 下一篇自己从0开始学习Unity的笔记 I (..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目