Skip to main content

dotnet

Scale a Kubernetes Deployment with .NET Core

·351 words·2 mins
Let’s start: Create a folder for your new project # Open a command prompt an run: mkdir kuberenetes.scale Create the project # cd kuberenetes.scale dotnet new api Add the references to KubernetesClient # dotnet add package KubernetesClient -v 1.5.18 dotnet restore Create a PodsController.cs with the following code # using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using k8s; using k8s.Models; using Microsoft.AspNetCore.JsonPatch; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; namespace kubernetes.scale { [Route("api/[controller]")] [ApiController] public class PodsController : ControllerBase { private KubernetesClientConfiguration k8sConfig = null; public PodsController(IConfiguration config) { // Reading configuration to know if running inside a cluster or in local mode. var useKubeConfig = bool.Parse(config["UseKubeConfig"]); if (!useKubeConfig) { // Running inside a k8s cluser k8sConfig = KubernetesClientConfiguration.InClusterConfig(); } else { // Running on dev machine k8sConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile(); } } [HttpPatch("scale")] public IActionResult Scale([FromBody]ReplicaRequest request) { // Use the config object to create a client. using (var client = new Kubernetes(k8sConfig)) { // Create a json patch for the replicas var jsonPatch = new JsonPatchDocument<V1Scale>(); // Set the new number of repplcias jsonPatch.Replace(e => e.Spec.Replicas, request.Replicas); // Creat the patch var patch = new V1Patch(jsonPatch); // Patch the "minions" Deployment in the "default" namespace client.PatchNamespacedDeploymentScale(patch, request.Deployment, request.Namespace); return NoContent(); } } } public class ReplicaRequest { public string Deployment { get; set; } public string Namespace { get; set; } public int Replicas { get; set; } } } Replace the contents of the appsettings.Development.json file # Note the UseKubeConfig property is set to true.

Updated Step by step: Serilog with ASP.NET Core

·276 words·2 mins
Many of you come to my site to read the post Step by step: Serilog with ASP.NET Core which I wrote in 2016 and is completely out of date, so with this post I will show you how to setup Serilog to work with your ASP.NET Core 2.2 applications. Create an ASP.NET Core project # md aspnet.serilog.sample cd aspnet.serilog.sample dotnet new mvc Add the following dependencies to your project # dotnet add package Serilog.AspNetCore dotnet add package Serilog.Extensions.Logging dotnet add package Serilog.Sinks.ColoredConsole Change your Program.cs file to look like the following # using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Serilog; using Serilog.Core; using Serilog.Events; namespace aspnet.serilog.sample { public class Program { public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .MinimumLevel.Debug() .WriteTo.ColoredConsole( LogEventLevel.Verbose, "{NewLine}{Timestamp:HH:mm:ss} [{Level}] ({CorrelationToken}) {Message}{NewLine}{Exception}") .CreateLogger(); try { CreateWebHostBuilder(args).Build().Run(); } finally { Log.CloseAndFlush(); } } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseSerilog() .UseStartup<Startup>(); } } Inject the logger to your services or controllers # Change the home controller and log some actions:

Develop and build ASP.NET Core applications to run on Kubernetes with Draft

You start developing an ASP.NET Core application to run it in Kubernetes and suddenly you find yourself creating a docker file, building an image, pushing the image to a registry, creating both a deployment and a service definition for Kubernetes and you wonder if there is a tool out there to help you streamline the whole process.

Revisiting Docker Multi Stage Builds to build an ASP.NET Core Echo Server

·252 words·2 mins
On April I wrote a post about Using Docker Multi Stage Builds to build an ASP.NET Core Echo Server and these days while preparing a talk, on CI/CD and kubernetes, I started to play with the simple sample I wrote back then. Soon enough I noticed that with each docker build command I issued the dependencies for the echoserver were always restored, even if no changes were made to the project file (csproj).

Adding SourceLink to your .NET Core Library

·500 words·3 mins
Last week I read this tweets from Maxime Rouiller (@MaximRouiller): Are you an MVP with a #dotnetcore #nuget package? Are you looking for an easy blog post? I have something for you. — Maxime Rouiller (@MaximRouiller) August 22, 2018 Actually, you're already using it. https://t.co/N5IaY6TGqQ That project is freaking cool. We need more package author to use it.

.NET Core, BenchmarkDotNet: for vs foreach performance

·581 words·3 mins
So what is faster: looping through a List<> with for or with foreach? Today I’ll show you how to use BenchmarkDotNet with .Net Core to answer that question. Let’s start: Create a folder for your new project # Open a command prompt an run:

.NET Core, BenchmarkDotNet and string compare performance

·489 words·3 mins
You have to choose between using string.compare or == to compare strings. How would you know which method performs faster? Today I’ll show you how to use BenchmarkDotNet with .Net Core to answer that question. Let’s start: Create a folder for your new project # Open a command prompt an run:

Simple Machine Learning with .NET Core Sample

·493 words·3 mins
I think that more than a year and a half ago I read “Real-World Machine Learning” by Henrik Brink, Joseph Richards, and Mark Fetherolf. A book that is easy to read and goes “to the point”!!! I’m sure you know what I mean. At the time the only thing that prevented me from really enjoying the book samples was that there was no easy way to “translate” them to .NET Core.

Using Docker Multi Stage Builds to build an ASP.NET Core Echo Server

·390 words·2 mins
Today I’ll show you how to create a simple Echo Server with ASP.NET Core and then a Docker Image using multi-stage build: Create the Application # Open a PowerShell promt and run: mkdir echoserver cd echoserver dotnet new console dotnet add package Microsoft.AspNetCore -v 2.0.2 Replace the contents of Program.cs # Replace the contents of the Program.cs file with the following code:

Microsoft MVP Global Summit 2018 Experience

·375 words·2 mins
Disclaimer: For those of you who are expecting to learn or read about new technical information and cool new product features or roadmaps I have bad news: almost everything that Microsoft shares with MVPs during the Summit is under an NDA (Non-Disclosure Agreement) which means that what I’ve learned in Redmond stays with me, inside my head.

Run a Durable Azure Function in a Container

·830 words·4 mins
Greetings readers! Hope you all a Happy New Year! Last post I was about running a Precompiled .NET Core Azure Function in a Container. This time let’s go one step further and Run a Durable Azure Function in a Container Prerequisites: Docker installed and basic knowledge. .NET Core Azure Storage Account Azure Durable Functions Knowledge Create a .NET Core lib project # Create a .NET Core lib project.