Posts

Retrieving Services from Dependency Injection in .NET

Introduction Dependency Injection  (DI) is a critical part of .NET since the .NET Core days. A simple, although powerful, DI container is included out of the box, and ASP.NET Core makes heavy use of it . I already wrote about DI in .NET a few times: .NET 8 Dependency Injection Changes: Keyed Services ASP.NET Core Inversion of Control and Dependency Injection The Evolution of .NET Dependency Resolution Dependency Injection Lifetime Validation .NET Core Service Provider Gotchas and Less-Known Features An Extended Service Provider for .NET This time I want to highlight something that people may not be aware of, even though I already mentioned it in one of my posts. Retrieving Services As you know, the DI is represented by an instance of IServiceProvider . When we want to retrieve a service we call  GetService , passing it a Type , for a dynamic call, or  GetService<T> , for a strongly-typed version, which just calls the other one with a cast. Now, if the service repre...

Entity Framework Core Pitfalls: Asynchronous vs Synchronous Calls and Interceptors

Introduction Another on my EF Core pitfalls series. This time, interceptors and synchronous/asynchronous calls. Problem You surely know about EF Core interceptors , a very useful feature that I talked about many times. What's the problem with them? Well, all of the interceptor interfaces  define both synchronous as well as asynchronous versions of their methods. This is so that, for example, when we call DbContext.SaveChanges the synchronous version is called, and when it's DbContext.SaveChangesAsync , the asynchronous one instead. I think you get the idea. So, imagine we added an interceptor, inheriting from SaveChangesInterceptor ,   to our collection of interceptors . If we then commit the changes in the  DbContext  through a call to  DbContext.SaveChanges , only SavingChanges , SavedChanges , SavedChangesFailed ,  SaveChangedCanceled  and, or, ThrowingConcurrencyException methods will be called, and you may be very surprised because your well-cra...

Microsoft MVP Global Summit

Image
Today begins the Microsoft MVP Global Summit ! This is one of the highlights of being an MVP : a yearly gathering at the Microsoft Campus in Redmond, WA, to which all MVPs are invited. It's a chance to learn about the Microsoft products, where they are going, ask questions and provide feedback, discuss technical and business issues with the Microsoft personnel and other MVPs, and, it is all in all, a great experience! All under NDA, of course! ;-) This year, sadly, I won't be able to attend, but I wish all my MVP fellows a terrific Summit! #MVPBuzz #MVPAward #MVPSummit

A Simple State Machine in .NET

Image
Introduction This is another post that should be tagged lazy-weekend! Let's imagine a simple state machine for tracking the state of tickets. It consists of the following states: Created : the ticket has been created Ready : the ticket is ready to be picked up for implementation In Progress : the ticket is being implemented Blocked : the ticket is currently blocked In Review : the ticket was sent for review Closed : the ticket has been closed The following diagram illustrates these states and the possible transitions: A simple state machine Don't worry too much about the actual states, this is meant to be an example. An explanation for these transitions is in order: The first state for a ticket is Created From Created , the ticket can transition to Closed , or to Ready From Ready , the ticket can transition to In Progress , Blocked , or Closed   From In Progress , the ticket can transition to Blocked , In Review , Ready , or Closed From In Review , a ticket can transition to In...

.NET Metrics

Image
Introduction I recently posted about OpenTelemetry , and I mentioned metrics there, as it's an important part of observability . On this post I will dig a bit more about metrics, what they are and how they are used by .NET and ASP.NET Core. Very important, these definitions match the observability/ OpenTelemetry  standard, specifically, the Metrics API , and are designed to work together. Creating Metrics There are different kinds of metrics, and they are all usually created from the  Meter  class, part of the .NET System.Diagnostics.Metrics API. A  Meter  has a name ( Name ), an optional scope ( Scope ), an optional version ( Version ), and some optional tags ( Tags ), all unchangeable and initialised at constructor time, except tags. Besides this, it has build methods for all supported meters/instrument types. It can be constructed directly: var meter = new Meter("Some.Meter", version: "1.0.0"); The  Meter  class is disposable , so make sure you dis...

Old Blog Inaccessible

My original blog at weblogs.asp.net ,  https://weblogs.asp.net/ricardoperes , has been experiencing some severe issues as of last week, as the result, it is currently inaccessible. I was told by Neudesic that they are working on it, and no loss is expected. Apologies to all readers, but it's beyond me. Fingers crossed! Update: it's back again !

ASP.NET Core Extension Points - MVC

Introduction This is post #2 on my series of posts on ASP.NET Core extensibility. The first one is this one , on the core extension points. This time, I'm going to focus on extensibility that is specific to MVC . I'm covering here: Controller factories and activators Action invokers, action invoker factories and action invoker providers Action constraints and action contraint factories Value providers and value provider factories Input and output formatters Output cache Controller Factories and Activators A controller factory is what creates a controller instance and also takes care of releasing (disposing of) it. A controller activator does similar things, even the methods they expose are very similar, as we can see it from the controller factory  IControllerFactory  and the controller activator  IControllerActivator  interfaces. As it is, the default controller factory class,  DefaultControllerFactory , receives a controller activator from Dependency Injection...