Files
csharp/examples/webApiDependencyInjection/Controllers/ExampleDependencyInjectionOnMethodController.cs
Manuel Menegazzo 39e7d98c36 ASP.NET core web api DI example (#950)
* Added example source code

* Removed unnecessary nuget package
2022-07-13 16:36:56 -07:00

26 lines
867 B
C#

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