cloud-foundryasp.net-core-2.1pcfconfigserversteeltoe

Unable to read property key value from GIT repository using Config Server Services in Steeltoe


I am not able read property value from git in steeltoe for dot net core 2.1 application.

From my client application, I want to read properties file which is present in Git repository for different environment properties files like foo-development.properties/foo-Production.properties based on environment of the application.

Find my below Code for reading data

Program.cs:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
      WebHost.CreateDefaultBuilder(args)
    .UseCloudFoundryHosting(5000)
    .ConfigureAppConfiguration(b => b.AddConfigServer(new LoggerFactory().AddConsole(LogLevel.Trace)))
    .AddCloudFoundry()   
    .UseUnityServiceProvider()
    .UseStartup<Startup>();

Startup Page

public void ConfigureServices(IServiceCollection services)
    {
        services.AddOptions();
        services.ConfigureConfigServerClientOptions(Configuration);
        services.AddConfiguration(Configuration);
        services.ConfigureCloudFoundryOptions(Configuration);           

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddCors();
        services.AddDiscoveryClient(Configuration);

        //The following setting is tp read config values from json to class
        services.Configure<ConfigSettings>(Configuration.GetSection("ConfigSettings"));

        // Adds the configuration data POCO configured with data returned from the Spring Cloud Config Server
        services.Configure<ConfigServerData>(Configuration);

        services.AddScoped<ApiExceptionFilter>();
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "Alcatraz AddUrlLink Service", Version = "v1" });
        });
    }

Controller page

public class HomeController
{
    private IOptionsSnapshot<ConfigServerData> IConfigServerData { get; set; }
    private CloudFoundryServicesOptions CloudFoundryServices { get; set; }
    private CloudFoundryApplicationOptions CloudFoundryApplication { get; set; }
    private IConfigurationRoot Config { get; set; }
    public HomeController(IOptionsSnapshot<ConfigServerData> configServerData, IConfigurationRoot config,
        IOptions<CloudFoundryApplicationOptions> appOptions,
        IOptions<CloudFoundryServicesOptions> servOptions)
    {
        if (configServerData != null)
            IConfigServerData = configServerData;

        // The ASP.NET DI mechanism injects these as well, see
        // public void ConfigureServices(IServiceCollection services) in Startup class
        if (servOptions != null)
            CloudFoundryServices = servOptions.Value;
        if (appOptions != null)
            CloudFoundryApplication = appOptions.Value;

        _service = service;
        Config = config;
        CreateConfigServerDataViewData();
    }

    private void CreateConfigServerDataViewData()
    {
        Console.WriteLine("Started...");
        // IConfigServerData property is set to a IOptionsSnapshot<ConfigServerData> that has been
        // initialized with the configuration data returned from the Spring Cloud Config Server
        if (IConfigServerData != null && IConfigServerData.Value != null)
        {
            try
            {
                if (IConfigServerData != null)
                {
                    Console.WriteLine("Not Null" + IConfigServerData.ToString());
                }
            }
            catch(System.Exception ex) { }

            var data = IConfigServerData.Value; 

            if (IConfigServerData.Value != null)
            {

                Console.WriteLine("Propertis " + data);
                Console.WriteLine("Propertis " + data.foo);
            }
            Console.WriteLine("foo1 " + Config["Foo"]);
            Console.WriteLine("foo2 " + Config["foo"]);
        }
        else
        {
            Console.WriteLine("There is no Properties Files available");
        }

    }
}

Class files

public class ConfigServerData
{
    public string Bar { get; set; }
    public string foo { get; set; }

    // Optional data from vault
    public string Vault { get; set; }
}

myclient.properties & myclient-development.properties files in Git repository

foo: Test

I have committed this files in defined git URL. but I'm unable to get message property in my client application.

Kindly please help me on this.Thanks in advance.


Solution

  • Spring Cloud Config Server on PCF requires interactions to be authorized. In version 2.1 of Steeltoe, that authorization is not performed in Steeltoe.Extensions.ConfigServer*, but is performed in Pivotal.Extensions.ConfigServer*

    For your ConfigServer NuGet reference and using statement in program.cs, change "Steeltoe" to "Pivotal" and you should be all set.

    With this change, you can also remove the.AddCloudFoundry on WebHostBuilder as the Pivotal version of the Cloud Foundry Config Provider will add that for you.