Archive for the .NET Core category

Vaughan

Creating a CSV text to model custom model binder in ASP.NET Core

.NET Core

ASP.NET Core has quite a large selection of model binders built in that are able to convert http requests to simple and complex models in your actions. If there isn\’t one that matches your needs, you can always create a custom one. To show how, I\’m going to create a custom model binder that converts […]

Read more
Vaughan

Using middleware to conditionally allow endpoints

.NET Core

There are times when you have controllers or actions that are very useful for debugging issues but you don\’t really want to expose them in a production environment. One way to achieve this is to take advantage of the separation of the Endpoint Routing Middleware and the Endpoint Middleware components. Another way would be to […]

Read more
Vaughan

Why you should use the IHttpClientFactory in ASP.NET Core

.NET Core

One surprising issue when using the HttpClient, is the issue of port exhaustion. This often happens when an application creates a lot of HttpClient instances for short requests. When there is sufficient load the machine may run out of tcp ports and then it won\’t be able to make another request until a port opens […]

Read more
Vaughan

Using automapper in ASP.NET Core

.NET Core, Automapper

To be honest I have mixed feelings on using something like automapper by default but there are definitely cases when it heavily reduces the amount of code you write and therefore your bugs. It has pretty good integration with ASP.NET Core so when it should be used, it won\’t take long to get started. To […]

Read more
Vaughan

ASP.NET Core: Injecting dependencies directly into controller actions

.NET Core

One feature in ASP.NET Core that I was surprised by is the ability to inject a service into a specific action of a controller. Normally you inject into a constructor of a controller and keep the dependencies as local variables. So imagine a controller like this: [Route(api/[controller])] [ApiController] public class PersonController : ControllerBase { private […]

Read more
Vaughan

ASP.NET Core: Using fluent validations in your api controllers

.NET Core

If you need to validate the inputs to your api actions I really do recommend trying the FluentValidation.AspNetCore package. Its very easy to integrate and you can do quite powerful validations. As an example I created a Person controller. [Route(api/[controller])] [ApiController] public class PersonController : ControllerBase { [HttpPost] public async Task<ActionResult> Post([FromBody] PersonViewModel person) { […]

Read more
Vaughan

Semantic logging with ASP.NET Core

.NET Core

One subtle adjustment that some of the older .net developers most probably need to make is that idea of semantic logging in ASP.NET core. This is subtle because a lot of us started used string interpolation a few years ago. The examples in the documentation look very similar to how my string interpolation strings look. […]

Read more
Vaughan

ASP.NET Core IOptions Validation

.NET Core

I\’ve spoken previously about IOptions in ASP.NET core. On their own, they are a very useful way to inject configuration into your services. What I didn\’t mention is that they allow you to build in validation of your configuration. One reason that this might be needed is that the IOptions services are normally very forgiving […]

Read more
Vaughan

ASP.NET Core dependency injection: Registering open generics

.NET Core

A really interesting trick you can do with the ASP.NET core dependency injection is that you can register an open generic type. The prime example is the ILogger interface that is built in. Each class can inject a ILogger of its type without having to register it specifically. It can be quite a powerful technique […]

Read more
Vaughan

ASP.Net Core Hosted services

.NET Core

In the containerized application and Kubernetes world, quite often you need to create services that aren\’t traditional rest endpoints. Your application could for example rather listen to a message queue to process requests. It would be nice to still have all the nice infrastructure of .NET Core applications and things like health checks but have […]

Read more

BMC logoBuy me a coffee