* Add support for HttpClientFactory with KubernetesClientConfiguration * Add an example with http client factory * ensure sample uses the latest version of C#
This commit is contained in:
committed by
Kubernetes Prow Robot
parent
fcd8c166da
commit
e00f67abd4
46
examples/httpClientFactory/Program.cs
Normal file
46
examples/httpClientFactory/Program.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using k8s;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace httpClientFactory
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
// Learn more about generic hosts at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host
|
||||
using (var host = new HostBuilder()
|
||||
.ConfigureLogging((logging) =>
|
||||
{
|
||||
logging.AddConsole();
|
||||
})
|
||||
.ConfigureServices((hostBuilderContext, services) =>
|
||||
{
|
||||
// Ideally this config would be read from the .net core config constructs,
|
||||
// but that has not been implemented in the KubernetesClient library at
|
||||
// the time this sample was created.
|
||||
var config = KubernetesClientConfiguration.BuildDefaultConfig();
|
||||
services.AddSingleton(config);
|
||||
|
||||
// Setup the http client
|
||||
services.AddHttpClient("K8s")
|
||||
.AddTypedClient<IKubernetes>((httpClient, serviceProvider) =>
|
||||
{
|
||||
return new Kubernetes(
|
||||
serviceProvider.GetRequiredService<KubernetesClientConfiguration>(),
|
||||
httpClient);
|
||||
});
|
||||
|
||||
// Add the class that uses the client
|
||||
services.AddHostedService<PodListHostedService>();
|
||||
})
|
||||
.Build())
|
||||
{
|
||||
await host.StartAsync().ConfigureAwait(false);
|
||||
await host.StopAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user