C# 带有asp.net核心的servicestack读取web.config
如何使用ServiceStack ASP.Net Core读取appsettings.json或web.configC# 带有asp.net核心的servicestack读取web.config,c#,asp.net-core,web-config,
servicestack,C#,Asp.net Core,Web Config,
servicestack,如何使用ServiceStack ASP.Net Core读取appsettings.json或web.config IAppSettings appSettings = new AppSettings(); appSettings.Get<string>("Hello"); IAppSettings-appSettings=new-appSettings(); appSettings.Get(“Hello”); 没有找到任何东西 ServiceStack的.NET Core的默
IAppSettings appSettings = new AppSettings();
appSettings.Get<string>("Hello");
IAppSettings-appSettings=new-appSettings();
appSettings.Get(“Hello”);
没有找到任何东西 ServiceStack的.NET Core的默认AppSettings可以读取
,这是一个使用此选项的示例项目
使用.NET Core的IConfiguration配置模型
现在,您可以选择将.NET Core的IConfiguration
模型与新的NetCoreAppSettings
适配器一起使用
当使用推荐的.NET Core 2.0启动配置运行.NET Core应用程序时,.NET Core的IConfiguration
类会自动预配置,即:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
然后,您可以让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()
API绑定到复杂类型
一个使用它的.NET Core 2.0 ServiceStack v5项目示例是。@BrianOgden否它使用了一个完全不同的AppSettings impl,但您应该能够感谢,我并没有真正遵循它,我只是尝试执行新的JwtAuthProviderReader{HashAlgorithm=“RS256”,AuthKeyBase64=//从AppSettings.json获取值}在我的AppHost.cs文件中,我无法获得任何配置values@BrianOgden您仍然需要像平常一样使用它,您可以在.NET Core的IOC中注册,或者将其分配给静态变量或AppHost实例变量,等等,你是说我需要使用属性依赖项注入将一个水合实例IOptions添加到我的AppHost类中?类似于:public Microsoft.Extensions.Options.IOptions Settings{get;set;}@BrianOgden您可以将IConfiguration
本身注册为另一个依赖项。请参阅更新的答案,以说明如何在ServiceStack v5中使用新的NetCoreAppSettings
AppSettings适配器,该适配器现已在MyGet上提供。请在升级之前查看。
public class AppHost : AppHostBase
{
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false)
});
}
}