asp.net-core.net-coreconfigurationsteeltoe

Using Steeltoe Discovery Client on .NET Core console application (Spring Cloud Config)


In this thread I was able to setup my simple console application using ASP.NET CORE's configuration system.

The code is as simple as:

static void Main(string[] args)
{
    string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

    var configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{environment}.json", optional: false)
        .AddEnvironmentVariables();

    config = configuration.Build();


    var serviceProvider = new ServiceCollection()
        .AddSingleton<IConfiguration>(config)
        .AddDiscoveryClient(config)
        .BuildServiceProvider();

    Console.WriteLine(config["Test"]);

    Console.Read();
}

However, since the application does not use IApplicationBuilder, I cannot invoke the .UseDiscoveryClient() method. I end up receiving an error on .AddDiscoveryClient(config):

"Discovery client type UNKNOWN, check configuration"

Is there a work around this? We would like to experiment using console applications with our Spring Cloud Config server. If there is no way to do it with Steeltoe, feel free to inform with other libraries that do.


Solution

  • The extension methods AddDiscoveryClient and UseDiscoveryClient are for use with Steeltoe service discovery. The error message you're seeing is due to Steeltoe not knowing which type of service registry your app should be a client of (ie: "client type UNKNOWN").

    You only wish to access Spring Cloud Config server, so you don't need either of those methods. You can add the ConfigServerConfigurationProvider to you your config builder with .AddConfigServer.