Skip to content

Category: C#

Creating a dotnet core windows service

Services are great for performing reoccurring tasks, especially if the task needs to be repeated often, say every minute. They also have the benefit of not requiring anyone to be logged in to run the service. Luckily with dot net core, you can have one set up and running very easily with the help of TopShelf. To get started, create a new console application using the CLI. dotnet new console –name MyService Import the TopShelf NuGet into your .csproj file like so In your program file, configure and run a new service host. In the example below I create my…

Health checks in .netCore web APIs

Previously I have been writing health check controllers by hand. Now .netCore has a simple way of managing service health checks via middleware. You can test everything is operational all the way down to the database. You can even write your own custom checks in a few lines. A simple service operational health check can be enabled by adding two lines to your startup.cs file. One in the services configuration and one in the application configuration. The “/health” parameter specifies the route to be used for the health check. When navigating to the route, a message will be displayed on…

NUnit test cases

Testing multiple scenarios through the same function can be a bit of a time sink. Writing out a method for each scenario and just changing the data passed into the function then checking the output against the expected result. Steve Fenton showed me NUnit test cases the other day though which make running multiple permutations of data against a result set a breeze! They have been around a while I think, but I somehow missed that boat. See below for a simple example of how they work. The test above will check multiple combinations of inputs against the expected output…

C# Interfaces and default method implementations

The upcoming C# 8.0 release came up the other day, or more specifically the inclusion of default implementations on interface methods. At first glance it seems akin to madness, blurring the line between implementation and inheritance. We already have abstract classes that cater for this, so why the need? Taking a little time to dig a little deeper though, and I think it does make sense and could be extremely powerful. The caveat being that it isn’t abused, which it most certainly could be. Using it looks to be pretty straight forward. You simply provide a method body on the…