WEB API 2 filters on .net 5 to handle Exceptions

Hello my dear developers!

How do you feel today?

I’ve seen many times web API’s controller’s methods with ActionResult as return types. On these methods there are lots of defensive code so an appropriate status response can be generated. A neat trick to avoid crowding your controller methods handling exceptions is to user filters.

Filters are part of asp.net core pipeline, a middleware to handle recurring situation.

Filters in ASP.NET Core | Microsoft Docs

I based my solution on the following article since is a simple explanation (in net core 2.1 though):

Using ExceptionFilter for exception handling in AspNet Core Web API – Janak’s blog

I basically throw a domain exception an handle at in my own ExceptionFilter:

DoTasksBeforeReady/ExceptionFilter.cs at main · ambsenyestudi/DoTasksBeforeReady (github.com)

You just need to add it at Startup.cs on ConfigureServices and you are done

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers(config=> {
        config.Filters.Add(typeof(ExceptionFilter));
    });
    ...
}

Happy coding and remember…there is life beyond coding🙂

Testing with http client

Hello my dear developers!

How do you feel today?

I was trying to mock httpclient o use some sort of trick to test it. Found nothing and then decided to write my own HttpMessageHandler class.

While trying to look at the docs to be able to pass a factory function at constructor, I found this very interesting article of a guy that already did it.

https://dev.to/n_develop/mocking-the-httpclient-in-net-core-with-nsubstitute-k4j

I hope you enjoy this article as much as I did.

Happy coding and remember…there is life beyond coding🙂

Identity server 4 and overly heavy enties on Entity Framework

Hello my dear developers!

How do you feel today?

People always complain about Identity server make entities overly heavy, so I found the following article that explain how to rewrite all our store so you can make your own types for identity server much leaner and meaner.

https://www.scottbrady91.com/Identity-Server/Creating-Your-Own-IdentityServer4-Storage-Library

I hope you enjoy it as much as I did

 

Happy coding and remember…there is life beyond coding🙂

Custom User Manager Identity Server

Hello my dear developers!

How do you feel today?

Lately I’m really enjoying Identity Server 4, but I’m not so fond of the user classes so here is nice article on how to go around user class and storage provider.

https://sikorsky.pro/en/blog/aspnet-core-custom-user-manager

I hope you enjoy it as much as I did.

Happy coding and remember…there is life beyond coding🙂

Open API (Swagger) and fluent validation

Hello my dear developers!

How do you feel today?

Continuing with the spirit of validating models easily on API started at:

https://afreixasblog.wordpress.com/2019/11/01/stop-validations-in-code-and-use-dataannotation/

I found this awesome article that uses fluent api on swagger to document your model

https://www.anexinet.com/blog/asp-net-core-fluentvalidation-swagger/

I hope you enjoy this article as much as I did.

Happy coding and remember…there is life beyond coding🙂

Easiest token-based authetication

Hello my dear developers!

How do you feel today?

Working with our client mention on last week’s article, they realized that token-based authentication is what they needed. We got them at it can expire every hour and be refreshed by a refresh token.

They had problems of multiple users using the same credentials in the past, so we had to use their authentication system as login and then use token-based authentication from that point on.

A read this piece that explains the whole thing succinctly:

https://dotnetthoughts.net/token-based-authentication-in-aspnet-core/

It is also important and not metioned in former article to specify the Authorize scheme or your token authenticaiton won’t work. It took me a while to figure it out.

Here at Microsoft docs they explain how:

https://docs.microsoft.com/es-es/aspnet/core/security/authorization/limitingidentitybyscheme?view=aspnetcore-3.1

I hope you enjoy this piece as much as I did

Happy coding and remember…there is life beyond coding🙂

Simplest authentication

Hello my dear developers!

How do you feel today?

Last week I came across a client that had a very peculiar authentication system in place. Since we are migrating their web services to a dot net core 3.0 API I had to come up with some system to use what they have instead of any of all the wonderful existing solutions out there.

I found a great article:

https://jasonwatmore.com/post/2019/10/21/aspnet-core-3-basic-authentication-tutorial-with-example-api

Promise to have an implementation on my github account, because it is not as out of the box as one would like it to.

I hope you enjoy this piece as much as I did

Happy coding and remember…there is life beyond coding🙂

Rest API Exception handling in Asp.net core using built-in middleware

Hello dear developers and software crafters!
How do you feel today?

Some time ago I was working on an API project that had to consume lots of micro services that had responses serialized in JSON. Since I was using async methods I was implementing the typical tuple success, Object methods.

Out of curiosity I asked developers from other teams how they handled the Rest response codes and exceptions while complaining about the verbosity of my solution. Then after many shrugs a colleague told mi about Asp.net core middleware to trap exceptions.

Here is a piece on the subject that explains the process

https://blogs.msdn.microsoft.com/brandonh/2017/07/31/using-middleware-to-trap-exceptions-in-asp-net-core/

I hope you enjoy this piece as much as I did.

Happy coding and remember… there is life beyond coding 😊

API good practices

Hello dear developers and software crafters!

How do you feel today?

Some time ago I was at a session on good practices for web API and something interesting was mentioned.

Why most web api prompt errors when their base URL is reached?

Isn’t it a bit smelly?

Speakers advice to redirect to Swagger on as the default page and found a nice stack overflow piece about.

https://stackoverflow.com/questions/30028736/how-to-use-swagger-as-welcome-page-of-iappbuilder-in-webapi

I hope you enjoy this piece as much as I did.

Happy coding and remember… there is life beyond coding 😊