Vaughan

Authenticating to IdentityServer4 in ASP.NET Core

.NET Core, IdentityServer4, OAuth

Authenticating to an IdentityServer4 service with an ASP.NET Core application is actually quite easy once you see it. It does require a basic understanding of the OpenID framework though. I’m first going to explain some basics and then I’ll show the code at the end. IdentityServer4 will expose a set of endpoints that a client […]

Read more
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 the swagger-codegen-cli Docker image to generate c# classes from a swagger api

Docker, Swagger

Not everyone knows that swagger gives you more than just a nice UI to expose your APIs. It actually exposes a JSON endpoint that defines your API. Tools like swagger codegen can use that endpoint to create a complete consuming project in an array of programming languages. Its a bit of a pain to install […]

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

BMC logoBuy me a coffee