Quantcast
Channel: Josef Ottosson
Browsing latest articles
Browse All 244 View Live

Image may be NSFW.
Clik here to view.

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 Article


Migrate 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 Article


Customize 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 Article

Streaming 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 Article

Using 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 Article


C# - 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 Article

Append 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 Article

Act 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 Article


UnsafeAccessor 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


Use 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 Article

Image may be NSFW.
Clik here to view.

Keeping 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 Article

Image may be NSFW.
Clik here to view.

Don'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 Article

System.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 Article


Image may be NSFW.
Clik here to view.

Automatically 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 Article

Image may be NSFW.
Clik here to view.

Stream 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

Browsing latest articles
Browse All 244 View Live