Posts

Entity Framework Core Pitfalls: Calling DB Functions in LINQ Queries as Extension Methods

Introduction Another one for my series on Entity Framework Core Pitfalls. Sometimes, even when using EF Core, we need to perform some operations, like calling a database (DB) function, that is not mapped to LINQ. We've been able to do this for a long time, and it is certainly the case with EF Core. Let's see how. Calling DB Functions in LINQ Queries So, some standard DB functions have been mapped to EF.Functions , as part of the DbFunctions class, but there are a big number of them that haven’t. Some examples include SQL Server's  REVERSE ,  SOUNDEX , DIFFERENCE , and many others. Now, there's a way to call them in LINQ queries, but there is a (minor) caveat. One way to do this is to have a method in your DbContext -derived class, which can be static or instance, such as: public string Reverse(string property) => throw new NotImplementedException("Only to be called in an EF Core LINQ query."); }; Now, we need to tell EF Core to use this function, and there...

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

2024 in Retrospective

This year was very important to me, professionally. I changed job , wrote 18 + 12 posts (in the old and the new blog ) - which, accidentally, means I got a new blog after 15 years with the old one -, went to some cool Microsoft events, and, finally, was awarded as Microsoft MVP once again ! Some things didn't work out exactly as I was expecting them to, but, in general, it was a very positive year. More news coming soon! I'd like to wish you all an excellent 2025!

.NET Cancellation Tokens

Introduction Cancellation tokens in .NET were introduced as part of the  .NET 4 Cancellation Framework . Essentially, it is a standard, cooperative, way to request for the cancellation of the execution of an operation. By cooperative it means that the code that uses it must abide to some rules, as opposed to the cancellation just stopping (killing) the execution. One example of cancellation is, on a web app, when the client closes the connection while the server is performing some long operation. In cases such as this, we may want to abort the operation, as we are not sending the results anywhere (or not, as we shall see). Because of this, ASP.NET Core allows us to add a parameter of type  CancellationToken  to our asynchronous actions, it is automatically associated with the client and is therefore signalled when the client connection is closed. A  CancellationToken  is, essentially, behind the scenes, a ManualResetEvent . Throughout this article I will either ...

What's New in .NET 9 and C# 13

Introduction .NET 9 and C# 13 have been released some days ago , so it's time for my own resume of the new features. There are tons of improvements, quite a few related to performance, so, beware, if you want the whole thing, you should read the official documentation , these are just my personal choices! Semi-Auto Properties C# 13 introduced a new (experimental as of now, you need to turn on the preview language features for the project) keyword called field , which can be used, in auto-properties , to access the auto-generated field.  To set the language version to preview , add this to your .csproj file: <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net9.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <LangVersion>preview</LangVersion> </PropertyGroup> And to make it work: public DateTime Date { get { return field; } ...