Testing your Polly policies
Photo by Reuben Introduction We have the following Polly policy: public static IAsyncPolicy<HttpResponseMessage> MyRetryPolicy( int attempts = 3, Func<int, TimeSpan>? sleepDurationProvider...
View ArticleMigrate your database with Entity Framework Core using a separate application
The problem We're using EF Core to handle our database. We're using it to migrate our database schema/data in all our environments. In our dev environments we also use it to seed and clear/reset the...
View ArticleCustomize the HttpClient logging - dotnet core
The problem When doing http calls using the HttpClient, out of the box you'll get the following log output: services.AddHttpClient("my-client", client => { client.BaseAddress = new...
View ArticleStreaming files to Postgres - dotnet core
Well, that's a title I never thought I would write. Anyhow, because of reasons, we're storing files in our database. This post will focus on the storing of the files. The next post will focus on...
View ArticleUsing the X-Forwarded-Prefix header to prefix your HATEOAS links in your API
Scenario All our requests towards our API goes through a BFF (think of it as a proxy). The client calls our BFF and the BFF proxies that call to our API. Now, our API knows nothing about the BFF, it...
View ArticleC# - Tips and Tricks 03 - Use static initialization
The problem I often see code like this: public class MyClass { public IReadOnlyCollection<string> MyStrings { get: } public MyClass() { MyStrings = new List<string> { "First", "Second",...
View ArticleAppend correlation id to all log entries in ASP.NET Core
Previously, I used Serilog to append a correlation ID to all my logs, like this: public class LoggingCorrelationIdMiddleware : IMiddleware { private const string CorrelationIdPropertyName =...
View ArticleAct on a Task as soon as it's completed with Task.WhenEach
You have the following code: public async Task RunJobs() { var jobs = Enumerable.Range(1, 20).Select(DoSomeWork); } // Simulates some "real" work private static async Task<(string JobName, TimeSpan...
View ArticleUnsafeAccessor is my new best friend
The problem For reasons I don't want to disclose, I really needed to access a private field in the RedisCache class in the Microsoft.Extensions.Caching.StackExchangeRedis package, namely the _cache...
View ArticleUse the implicit operator to reduce noise in your code
I often use Redis as a distributed cache in my applications. Redis is a key-value store. I prefer to have my cache keys typed; that is, I don't use plain strings throughout my codebase. Instead, I use...
View ArticleKeeping your nuget packages in check
Patch Tuesday / Dependency Day It's important to keep your dependencies up to date, as updates can contain security fixes, bug fixes, new features, and more. Microsoft (and other companies) have...
View ArticleDon't let ASP.NET Core set empty strings to null when posting forms
Recently, I’ve been involved in a project where an old ASP.NET Framework solution has been migrated to ASP.NET Core 8. Today, a bug report was filed: a form that was posted to one of the endpoints...
View ArticleSystem.Text.Json and immutable types - Deserialization
Imagine the following domain object for an Order: public class Order { private HashSet<OrderLine> Lines { get; init; } = []; private Order() { } public required Guid Id { get; init; } public...
View ArticleAutomatically format your dotnet code using dotnet format - GitHub Actions
Background When working in a team, it's important to follow the style guidelines that apply to the project. It makes it easier to read the code if all code "look and feel" the same. It should not be...
View ArticleStream files from Postgres - dotnet core
This is a follow up to my previous post Stream files to Postgres. Scenario Due to reasons I cannot reveal, I have a situation where I have some images stored in a Postgres database, in a bytea column....
View Article