ASP.NET core web api DI example (#950)
* Added example source code * Removed unnecessary nuget package
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
using k8s;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace webApiDependencyInjection.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class ExampleDependencyInjectionOnConstructorController : ControllerBase
|
||||
{
|
||||
private readonly IKubernetes kubernetesClient;
|
||||
|
||||
/// <summary>
|
||||
/// Inject the kubernets class in the constructor.
|
||||
/// </summary>
|
||||
/// <param name="kubernetesClient"></param>
|
||||
public ExampleDependencyInjectionOnConstructorController(IKubernetes kubernetesClient)
|
||||
{
|
||||
this.kubernetesClient = kubernetesClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example using the kubernetes client obtained from the constructor (this.kubernetesClient).
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet()]
|
||||
public IEnumerable<string> GetPods()
|
||||
{
|
||||
// Read the list of pods contained in default namespace
|
||||
var podList = this.kubernetesClient.CoreV1.ListNamespacedPod("default");
|
||||
|
||||
// Return names of pods
|
||||
return podList.Items.Select(pod => pod.Metadata.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using k8s;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace webApiDependencyInjection.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class ExampleDependencyInjectionOnMethodController : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Example using the kubernetes client injected directly into the method ([FromServices] IKubernetes kubernetesClient).
|
||||
/// </summary>
|
||||
/// <param name="kubernetes"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet()]
|
||||
public IEnumerable<string> GetPods([FromServices] IKubernetes kubernetesClient)
|
||||
{
|
||||
// Read the list of pods contained in default namespace
|
||||
var podList = kubernetesClient.CoreV1.ListNamespacedPod("default");
|
||||
|
||||
// Return names of pods
|
||||
return podList.Items.Select(pod => pod.Metadata.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
examples/webApiDependencyInjection/Program.cs
Normal file
34
examples/webApiDependencyInjection/Program.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using k8s;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Load kubernetes configuration
|
||||
var kubernetesClientConfig = KubernetesClientConfiguration.BuildDefaultConfig();
|
||||
|
||||
// Register Kubernetes client interface as sigleton
|
||||
builder.Services.AddSingleton<IKubernetes>(new Kubernetes(kubernetesClientConfig));
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
// Start the service
|
||||
app.Run();
|
||||
|
||||
|
||||
// Swagger ui can be accesse at: http://localhost:<yourBindingPort>/swagger
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
examples/webApiDependencyInjection/appsettings.json
Normal file
9
examples/webApiDependencyInjection/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user