ASP.NET Core – Client-side custom validation attributes

I wrote about how to add custom validation attributes. These are used for model validation on the server-side. You can also use these for client-side validation, which I’ll show in this article. 1 – Implement IClientModelValidator The first step is to implement the IClientModelValidator interface in the custom validation attribute class. This has a single … Read more

ASP.NET Core – Control the graceful shutdown time for background services

ASP.NET Core gives you a chance to gracefully shutdown your background services (such as cleaning up and/or finishing in-progress work). By default, you’re given a grand total of 5 seconds to shutdown all background services. If you need more time than this, there are a few ways to control the graceful shutdown time, which I’ll … Read more

Implement Interface with auto properties in VS

When you add an interface to a class, you can right-click the class and use the Implement Interface quick action to automatically implement the interface. By default, it implements members that throw exceptions, even the getters and setters: This is fine for methods, but you’d expect it to generate auto properties instead of properties that … Read more

C# – How to use SortedSet

When you have a collection of elements that you’re continuing to add to, and need to keep the objects in sorted order at all times, you can use SortedSet. Internally, it uses a tree data structure to keep elements in sorted order (O(log n) insertion). This is far more efficient than repeatedly sorting a list … Read more

C# – Default method implementations and constants in interfaces

You can add default method implementations (and constants) to interfaces (in C# 7+), like this: Note: The Log() method is converting the DateTime to a string with a custom format and including it with the logging message. This outputs the following: Microsoft’s stated purpose for this feature is that it allows you add methods to … Read more

C# – How to unit test code that uses HttpClient

When you want to unit test code that uses HttpClient, you’ll want to treat HttpClient like any other dependency: pass it into the code (aka dependency injection) and then mock it out in the unit tests. There are two approaches to mocking it out: In this article I’ll show examples of these two approaches. Untested … Read more

C# – Get all classes that implement interface

You can use reflection to get all classes in the current assembly that implement a specific interface. Here’s how: To create instances of these types, loop through them and use Activator.CreateInstance(), like so: Example – Auto-wire a command routing table Let’s say we want to build a command routing table. We have commands and want … Read more

How to mock static methods

The need to mock static methods in order to add a unit test is a very common problem. It’s often the case that these static methods are in third-party libraries. There are many utility libraries that are completely made up of static methods. While this makes them very easy to use, it makes them really … Read more