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>
|
||||||
@@ -65,7 +65,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KubernetesClient.Kubectl",
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kubectl.Tests", "tests\Kubectl.Tests\Kubectl.Tests.csproj", "{9128F6DC-6B7A-417F-937A-90461D6989A8}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Kubectl.Tests", "tests\Kubectl.Tests\Kubectl.Tests.csproj", "{9128F6DC-6B7A-417F-937A-90461D6989A8}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "openTelemetryConsole", "examples\openTelemetryConsole\openTelemetryConsole.csproj", "{8E266190-AE6E-44A8-948D-BD974AA82428}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "openTelemetryConsole", "examples\openTelemetryConsole\openTelemetryConsole.csproj", "{8E266190-AE6E-44A8-948D-BD974AA82428}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "webApiDependencyInjection", "examples\webApiDependencyInjection\webApiDependencyInjection.csproj", "{C0759F88-A010-4DEF-BD3B-E183D3328FFC}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@@ -425,6 +427,18 @@ Global
|
|||||||
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x64.Build.0 = Release|Any CPU
|
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x86.ActiveCfg = Release|Any CPU
|
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x86.Build.0 = Release|Any CPU
|
{8E266190-AE6E-44A8-948D-BD974AA82428}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@@ -459,6 +473,7 @@ Global
|
|||||||
{21201F30-5463-4FC6-93C3-FBF157F0D46C} = {3D1864AA-1FFC-4512-BB13-46055E410F73}
|
{21201F30-5463-4FC6-93C3-FBF157F0D46C} = {3D1864AA-1FFC-4512-BB13-46055E410F73}
|
||||||
{9128F6DC-6B7A-417F-937A-90461D6989A8} = {8AF4A5C2-F0CE-47D5-A4C5-FE4AB83CA509}
|
{9128F6DC-6B7A-417F-937A-90461D6989A8} = {8AF4A5C2-F0CE-47D5-A4C5-FE4AB83CA509}
|
||||||
{8E266190-AE6E-44A8-948D-BD974AA82428} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
|
{8E266190-AE6E-44A8-948D-BD974AA82428} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
|
||||||
|
{C0759F88-A010-4DEF-BD3B-E183D3328FFC} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7}
|
SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7}
|
||||||
|
|||||||
Reference in New Issue
Block a user