Search code examples
c#configurationsteeltoe

SteelToe not able to access data from config server


I have following the SteelToe documentation for accessing configuration data from my config server. https://steeltoe.io/docs/steeltoe-configuration/#2-2-5-access-configuration-data

Inside my TestController I setup the configuration global variable in the constructor. However when I check the variable _config it has no value basically null value.

I not sure if I need to physically map the values to the CustomConfig class properties? As this is not specified in the documentations.

Startup.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"));
}

Program.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>();
    }
}

Controller.cs

public class TestController : ControllerBase
{
     private Startup.CustomConfig _config;     
     public TestController(IOptions<Startup.CustomConfig> configSettings)
     {
          _config = configSettings.value; // this is null
     }
}

Solution

  • There are several things happening here that are in your way:

    1. Don't build configuration inside Startup, that should be handled in program.cs
    2. Property names need to match up when binding config to custom classes
    3. Options should be configured in Configure