我遵循SteelToe
从我的配置服务器访问配置数据的文档。https://steeltoe.io/docs/steeltoe-configuration/#2-2-5-access-configuration-data
在我的内部,我TestController
在构造函数中设置了配置全局变量。但是,当我检查变量时_config
,它基本上没有null
值。
我不确定是否需要将值物理映射到CustomConfig
类属性?因为这没有在文档中指定。
启动.cs
public class CustomConfig {
public string Message { get; set; }
}
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddConfiguration(configuration)
.AddCloudFoundry()
.AddEnvironmentVariables();
this.Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CustomConfig>(Configuration.GetSection("spring:cloud:config:uri"));
}
程序.cs
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.Build()
.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.AddConfigServer()
.UseStartup<Startup>();
}
}
控制器.cs
public class TestController : ControllerBase
{
private Startup.CustomConfig _config;
public TestController(IOptions<Startup.CustomConfig> configSettings)
{
_config = configSettings.value; // this is null
}
}