ASP.NET core web api DI example (#950)

* Added example source code

* Removed unnecessary nuget package
This commit is contained in:
Manuel Menegazzo
2022-07-14 01:36:56 +02:00
committed by GitHub
parent 6f323d82fd
commit 39e7d98c36
7 changed files with 139 additions and 1 deletions

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View 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

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View File

@@ -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>

View File

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