Union types as a Result - how it compares
A few years ago I wrote about my take on the Result class in C#, which eventually turned into JOS.Result, a nuget package I still use in pretty much all of my projects. The whole idea has always been:...
View ArticleBeware of ValueGeneratedOnAdd when using EF
Yesterday I wasted a few hours on the following code: foreach(var claim in claims) { if(ClaimType.TryParse(claim.Key, out var claimType)) { var claimValue = claim.Value.ToClaimValue();...
View ArticleAzure AppService - 502 Bad Gateway - HTTP/2
Just wasted a couple of hours of my life helping a colleague at work troubleshooting why we got a 502 response when trying to access our application in Azure. Regular ASP.NET Core 10 api, nothing...
View ArticleForce DbContext SaveChanges to throw exception during test
I have the following code: public async Task<Result<Guid>> HandleClient<T>(T client) where T : Client { var exists = await _dbContext.Clients.AnyAsync(c => c.ClientId ==...
View ArticleIDistributedCache (Redis) - remove by prefix
I'm a heavy user of Redis. On a recent project, we cached a bunch of content that had the following key structure: content-{company-id}-checkout-... The requirement was that we needed to drop all keys...
View ArticleCompress HttpClient requests with GZIP
In a project I'm part of, I found that we had some code that gzipped some requests before sending them to an external API. I found it because of some performance problems, especially with larger files....
View ArticleRead only properties on Domain object using Entity Framework Core
I'm all in on storing my domain objects in my database. Previously, I used specific "storage DTOs" and mapped between them. Nothing wrong with that, since that gives you full control. However, if we...
View ArticleJOS.Enumeration - Minimal API support
I have the following enumeration: ResponseType public partial class ResponseType : IEnumeration<string, ResponseType> { public static readonly ResponseType Code = new("code", "Code"); public...
View ArticleDon't reinvent the wheel - configuration
More often than not, I find code in various dotnet core solutions that contains code like this: Program.cs ....... if(EnvironmentHelper.IsLocal) { services.AddSingleton<ICustomerClient,...
View ArticleTesting 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 ArticleDon't reinvent the wheel - configuration
More often than not, I find code in various dotnet core solutions that contains code like this: Program.cs ....... if(EnvironmentHelper.IsLocal) { services.AddSingleton<ICustomerClient,...
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 ArticleUsing http/2 with ASP.NET Core - Traefik/Kubernetes/container example
Intro When dealing with TLS and containers, a common pattern is to terminate the TLS connection in a load balancer and then speak http/1 between the loadbalancer and the container. It has a number of...
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 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 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 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 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 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 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 Article