Posts

Showing posts from January, 2025

ASP.NET Core Middleware

Image
Introduction This post is another of those "back to basics", still, there was one thing or two that I feel were not properly discussed elsewhere, hence this post. The ASP.NET Core Pipeline and Middleware You may know that ASP.NET Core defines a pipeline. This is where the incoming requests are processed and the response submitted. Each component in the pipeline - a middleware - can do things with the message (the request), such as check if it is authenticated and have rights to access the requested resource, turn the request into an MVC-style controller and action invocation, log it, and what not. You can read more about it here . The ASP.NET Core pipeline, from Microsoft The order by which the middleware is added to the pipeline matters. For example, the authorisation middleware must come after the authentication one, static files must come before routing, and exception handling must come before everything. The middleware order, from Microsoft A middleware is just a piece of...

The Disposable Pattern in ASP.NET Core

Introduction When you use a class that implements the Dispose Pattern , or, in general, that implements IDisposable , you should know what to do with it, in regards to its lifetime. We typically want to keep it as short as possible, but that may not always be the case; in any case, we definitely want to dispose of it properly. Let's see some of the issues around this. Instantiation There are two ways by which we can instantiate our IDisposable objects: Using Dependency Injection (DI) Manually When using DI, we have three different possible lifetimes, which require different cares. DI Instantiation Scoped Disposal If we are to use DI, then, chances are, we will make use of the Scoped lifetime. This essentially means that the framework, at the end of the scope - the web request - will call its Dispose method automatically. Nothing really to be done. Transient Disposal For Transient instances registered on the DI container, we should create a scope on the DI container, and dispos...

ASP.NET Core Distributed Tracing

Image
Introduction Distributed tracing is a technique that allows us to detect failures and performance issues in distributed applications. For example, you send a request to a web app and this web app needs, in turn, to talk to one or more microservices, and possibly some of these microservices need also to talk to others. Distributed tracing is a way to correlate all these calls under the same umbrella. This is usually achieved through special request headers that must be present for distributed tracing to take place. ASP.NET Core offers mechanisms to make our lives easier when working with these headers, which is an implementation of the OpenTelemetry standard.  OpenTelemetry  is an observability framework and toolkit designed to create and manage telemetry data such as traces, metrics, and logs. For now, we'll focus on the building blocks of distributed tracing. You can think of a distributed trace as a transaction with an identifier and many operations, which are ocorring on ...