具有 asp.net 核心读取 web.config 的服务堆栈

servicestack with asp.net core read web.config

如何使用ServiceStack ASP.Net Core读取appsettings.jsonweb.config

IAppSettings appSettings = new AppSettings();
appSettings.Get<string>("Hello");

没有找到任何东西。

ServiceStack 的默认 AppSettings for .NET Core 可以读取 <appSettings/>SimpleAuth.Mvc web.config 是一个使用它的示例项目。

使用 .NET Core 的 IConfiguration 配置模型

使用 new ServiceStack v5 that's now available on MyGet you can choose to instead use .NET Core's IConfiguration model with the new NetCoreAppSettings IAppSettings 适配器。

.NET Core 的 IConfiguration class 在 运行 您的 .NET Core 应用程序使用推荐的 .NET Core 2.0 启动配置时自动预配置,即:

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}

你可以在哪里请求将它注入 Startup 构造函数并将它分配给一个 属性 with:

public class Startup
{
    public IConfiguration Configuration { get; }
    public Startup(IConfiguration configuration) => Configuration = configuration;

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseServiceStack(new AppHost
        {
            AppSettings = new NetCoreAppSettings(Configuration)
        });
    }
}

然后您可以让 ServiceStack 将其与 NetCoreAppSettings 适配器一起使用,如上所示。

这与正常工作一样 IAppSettings,您可以使用它来读取各个配置值,例如:

public class AppHost : AppHostBase
{
    public override void Configure(Container container)
    {
        SetConfig(new HostConfig
        {
            DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false)
        });
    }
}

或使用 IAppSettings.Get<T>() API 绑定到复杂类型。

使用它的示例 .NET Core 2.0 ServiceStack v5 项目是 NetCoreTemplates/react-spa