Friday, June 4, 2021

GraphQL– Optional arguments

Every field on a GraphQL object type can have zero or more arguments, for example the length field below:

An argument can be either required or optional. For optional arguments, you can specify a default value, like  METER in the example above.

To configure this using HotChocolate, you can use the following syntax:

Thursday, June 3, 2021

ASP.NET Core - Handle cancelled requests

When an HTTP request is made to your ASP.NET Core application, it is always possible that the request is aborted. For instance when the user closes it browsers without waiting for the response. In this case, you may want to stop all the work to avoid consuming resources.

Here are 2 possible approaches on how to handle this scenario. A first option is to check the HttpContext.RequestAborted property:

 
A second option is to let the Modelbinder do it’s work and add a parameter of type CancellationToken to the action:

Wednesday, June 2, 2021

Azure Kubernetes Service - Time sync issue between nodes

We encountered a strange issue this week inside our AKS cluster. We discovered that the time was not synced between the different pods and nodes.

We noticed this because we couldn’t use our  OAuth security tokens as the IssuedAt timing was off.

To validate this issue we ssh’d into the nodes and ran the following command:

$: sudo timedatectl status

This resulted in the following output

Local time: Wed 2021-6-2 13:48:44 UTC
Universal time: Wed 2021-6-2 13:48:44 UTC
RTC time: Wed 2021-6-2 13:48:44
Time zone: Etc/UTC (UTC, +0000)
Network time on: yes
NTP synchronized: no
RTC in local TZ: no

The NTP service was disabled and no NTP service was configured. To fix it we opened the timesyncd.conf:

$: sudo cat /etc/systemd/timesyncd.conf

and changed the NTP value

[Time]
NTP=ntp.ubuntu.com

After that we restarted the timesync service:

$: sudo timedatectl set-ntp true
$: sudo systemctl restart systemd-timesyncd.service

Of course this is only good as a temporary workaround. I would expect that this is enabled by default.

Tuesday, June 1, 2021

Azure Pipelines–Pipeline Resource Trigger

Yesterday I started blogging about my journey moving from the ‘classic’ build approach to YAML templates. I shared how you can use a Build completion trigger to link your YAML build to a previously completed build. Although this approach works, it is no longer recommended.

A better way is to use ‘Pipeline Resource Triggers’.  This is done by defining a pipelines resource inside your YAML template. pipelines is a dedicated resource only for Azure Pipelines. Let’s have a look at the syntax:

In your resource definition, pipeline is a unique value that you can use to reference the pipeline resource later on. source is the name of the pipeline that produces an artifact.

Remark: As I mentioned in the example above, the source name is case sensitive.

More information: https://docs.microsoft.com/en-us/azure/devops/pipelines/process/pipeline-triggers?view=azure-devops

Monday, May 31, 2021

Azure Pipelines–Build completion triggers

I’m currently migrating an existing CI/CD pipeline build in Azure DevOps from the ‘classic’ build approach to YAML templates. It was quite a journey. So expect a lot of posts the upcoming days about this topic where I share everything I learned along the way.

In our original setup we had an Azure DevOps classic pipeline that was used to create a Docker image and push it to ACR (“the CI part”). After that a release pipeline was triggered that took the image from ACR and deploys it in multiple AKS clusters (“the CD part”).

The goal was to keep this way of working and only make the switch from the ‘classic’ build approach to YAML templates. So the first thing I had to find out was how I could trigger the CD build when the CI build completed.

Let me first show you the approach that is most similar to the classic approach using ‘Build completion triggers’. Although this approach still works, it is no longer recommended. The recommended approach is to specify pipeline triggers directly within the YAML file but that is something for tomorrow.

Build completion triggers

Let’s walk through the steps required to use the Build completion trigger:

  • Open the YAML pipeline that you want to be triggered after build completion.
  • Click on ‘Edit’ to open the YAML pipeline in Edit mode

  • Click on the 3 dots and choose Triggers from the context menu

  • The Triggers screen is loaded. Here you can click on ‘+Add’ and select a build 

Thursday, May 27, 2021

NSwag error - System.InvalidOperationException: No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[Autofac.ContainerBuilder]' has been registered.

In one of our projects we are using NSwag to generate our TypeScript DTO’s and services used in our Angular frontend. In this project we are using Autofac as our IoC container and have created a few extension methods that hook into the HostBuilder bootstrapping.

Unfortunately our custom logic brought NSwag into trouble and caused our build to fail with the following error message:

System.InvalidOperationException: No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[Autofac.ContainerBuilder]' has been registered.

NSwag adds an extra build target in your csproj file and uses that to run the NSwag codegenerator tool:

While investigating the root cause of this issue, we introduced a small workaround where we used a separate program.cs and startup.cs specifically for the NSwag codegenerator.

We added a minimal Program.cs file:

And a minimal Startup.cs file:

The magic to make this work is to change the nswag.json configuration file in the root of our project and point to the alternative files we’ve created above:

"createWebHostBuilderMethod": "Example.App.Web:Example.App.Web.NSwag.NSwagProgram.FakeNSwagHostBuilder",

"startupType": "Example.App.Web:Example.App.Web.NSwag.NSwagStartup",

Although this is not a final solution, until I found the exact root cause, this will do the trick...

Wednesday, May 26, 2021

Microsoft Build 2021–Book of news

Microsoft continues it’s new tradition to bundle all important announcements in a ‘Book of news’. So ,if you don’t have the time to watch Satya Nadella's keynote or the Scott Hanselman and friends version of the keynote have a look at the long list of available sessions, the book of news is there to guarantee you are up to speed with the latest and greatest in the Microsoft ecosystem.