Posts

Injecting Action Method Values from Configuration in ASP.NET Core

Introduction ASP.NET Core offers some mechanisms by which it can automatically "inject", or set, values to action method parameters. Normally these come from the query string, the form, the payload, headers, or the Dependency Injection (DI), but the mechanism is extensible, and here we will see a way to inject values from the configuration. We will build: A binding source A model binder provider A model binder An extension method to ease the registration of the model binder provider An "injection" attribute to trigger the injection Model binding in ASP.NET Core is a complex topic and we won't cover it in depth, just what is needed to get this done with a custom model binder . What I'm going to present could probably be done with either a custom value provider or a custom model binder (as in this post), as they are related topics: the former is used to get values from sources and the latter to get them into .NET classes. I will go with model binding for now,...

How I Write Posts

This is a non-technical post, I wanted to explain a few things about my blogging. First, I usually write in British English . This is essentially because I work (and have worked in the past) for British companies, so this is the variant of English that I use daily. This has no other meaning than this. Second, I usually prepare my posts for a long time before they are actually published, but, still, things happen and I sometimes get things wrong, or wish to add/clarify something. So, I frequently make amends to posts, and sometimes I even signal them. My interests include .NET and ASP.NET Core, databases (SQL Server, Elasticsearch ), JavaScript, and web/distributed development in general. I probably won't write about Java, Go, or Python. I tend to avoid what I feel is unnecessary details, such as using directives , and the likes, and also generally keep error handling to the minimum. This doesn't mean that I don't care about it, it's just that it's usually not the po...

Text Querying with EF Core and SQL Server

Introduction SQL Server provides a number of functions that can be used for non-exact text queries over text columns ( CHAR , NCHAR , VARCHAR , NVARCHAR , TEXT , NTEXT ). These include searching a piece of text contained inside another, using full text search, or using patterns, and computing a code from a text. Some of them are: CONTAINS : Searches for precise or fuzzy (less precise) matches to single words and phrases, words within a certain distance of one another, or weighted matches in SQL Server DIFFERENCE : This function returns an integer value measuring the difference between the  SOUNDEX  values of two different character expressions FREETEXT : Is a predicate used in the Transact-SQL WHERE clause of a Transact-SQL SELECT statement to perform a SQL Server full-text search on full-text indexed columns containing character-based data types LIKE : Determines whether a specific character string matches a specified pattern PATINDEX : Returns the startin...

OpenTelemetry with ASP.NET Core

Image
Introduction I wrote a post not too long ago about the building blocks of telemetry , or distributed tracing , with ASP.NET Core. Now I'm going to talk about how we can see it working using an open source bundle, otel-lgtm . Mind you, this may not be 100% suitable for production, but for development purposes, it should be fine, all concepts apply, of course. I won't cover everything about OpenTelemetry here, but should hopefully give you some insights as to what's there and how you can make good use of it. First, lets see what some of the concepts are. OpenTelemetry OpenTelemetry  (OTel) provides a single, open-source standard, and a set of technologies to capture and export metrics, traces, and logs from your cloud-native applications and infrastructure. OTel is both a standard and a reference implementation that builds on other standards, such as W3C Distributed Tracing . OpenTelemetry is essentially a data collector that stores data that is sent seamlessly by web serv...

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