In this article, we will learn about 13 Libraries in ASP.NET Core every developer should know about. Of course, there are thousands of NuGet packages and libraries hosted and maintained under nuget.org and these are not only specific for ASP.NET, but for the whole .NET world.
As stated by Microsoft there are over 100,000 NuGet packages.
All these packages were developed and tons of them are still being maintained and updated, and their sole purpose is to assist the developers to build different types of .NET Software, web, mobile, desktop, cloud and so on.
This article shows the very important and mostly used libraries when building applications in ASP.NET Core, many of these libraries can also be used and are really helpful in other .NET applications.
Below is a quick overview for these 13 libraries in ASP.NET Core:
Microsoft.AspNetCore.Authentication.JwtBearer
Newtonsoft.Json
Swashbuckle.AspNetCore.Swagger
Serilog.AspNetCore
Microsoft.EntityFrameworkCore
FluentValidation.AspNetCore
Mapster
Microsoft.AspNetCore.MVC.Versioning
Refit
StackExchange.Redis
MediatR
Moq
So let's get started learning more about the 13 Libraries in ASP.NET Core every developer should know about:
Microsoft.AspNetCore.Authentication.JwtBearer
If you want to bootstrap a simple authentication/authorization server as part of your ASP.NET Core solution, then your best bet is to trust the official ASP.NET Core JwtBearer Authentication Library to do the job for you.
JWT Bearer can generate tokens based on the OpenID Connect Protocol.
This is mainly useful for the use cases of having a simple client that you want to authenticate.
However, if you are working on a multi-client products that range between public clients and confidential clients with different APIs and you want a more holistic strategy to apply authentication and authorization for all these products and APIs then it is better not to build your own authorization server.
Instead, you can rely on of the those mature identity providers like Auth0 or IdentityServer4 ( or Duende IdentityServer), these provide advanced implementations for identity and authorization based on OpenID Connect and OAuth 2.0 protocols and they have both free up to a specific number of implementations or requests or paid for above than that.
You just need to add a few configurations at the startup of your ASP.NET Core application, which is in the program.cs file.
Check my tutorial Apply JWT Access Tokens and Refresh Tokens in ASP.NET Core Web API, for a comprehensive article to learn how to implement JWT Authentication in ASP.NET Core Web API in .NET 7
JwtBearer NuGet Package Downloads: 338 M
Newtonsoft.Json
Newtonsoft.Json is the most popular NuGet library extending 3 Billion downloads, the library targets all versions of .NET, including .NET Framework up to 4.8, .NET Standard and .NET Core (up to .NET 7 and ongoing)
This allows you to easily serialize object to JSON string and vice-versa deserialize a JSON text to object a highly-performing manner.
Even though Newtonsoft.Json is the most used NuGet library, with the recent versions of .NET starting with .NET Core 3.1 and above, for most cases you can start using the built-in library System.Text.Json instead of NewtonSoft.Json.
Check this official migration guide by Microsoft to help you move your implementation from NewtonSoft.Json to System.Text.Json
Still if you haven't ever used a JSON formatting and serializing/deserliazing library then you can join the 3 Billion+ downloaders of Newtonsoft.json and try it out yourself.
Microsoft.AspNetCore.Mvc.NewtonsoftJson library can be used to do specialized formatting for the JSON responses returned by your ASP.NET Core Web API
Swashbuckle.AspNetCore.Swagger
While this comes bundled in your ASP.NET Core project starting in .NET 6, you must know about Swashbuckle which is an ASP.NET Core implementation for Swagger OpenAPI specifications.
By just having the Swashbuckle preloaded into your ASP.NET Core Web API, it will automatically generate an OpenAPI specification file in JSON format.
Swashbuckle along with Swagger allows you to add more configurations for your Web API specifications document to include authentication headers, predefined or custom headers, custom requests/responses and more.
Feel free to check my tutorial where I explain in detail about Swagger OpenAPI configurations in ASP.NET Core Web API
To learn more about Swashbuckle, check the official GitHub repo
Swashbuckle Swagger for ASP.NET Core NuGet package has reached over 397M downloads.
Serilog.AspNetCore
Serilog is a lightweight library that is super easy and simple to integrate with your ASP.NET Core Web API middleware pipeline to implement logging and allow you to write it to multiple sources (sinks), such as console, file, external database, external cloud service ...etc.
Also, you get the chance to format and customize the way you want your logs to appear and in which level of verbosity (Debug, Information, Warn, Error, or All).
Note that you can still write logs to files in ASP.NET Core using the ILogging Extensions Library that comes bundled with .NET 6 and above, however, if you want a more customized, extendable and structured logging solution, then you should definitely go with Serilog.
I wrote a dedicated tutorial about Logging with Serilog in ASP.NET Core Web API , check it out and let me know if you find it useful.
Check Serilog's GitHub repo to learn more about this amazing logging library.
Note that for ASP.NET Core application, you will need to install the Serilog.AspNetCore library not the Serilog library, since the former has extra setup configurations to enable the logging within the ASP.NET Core application and will install the Serilog library as part of its dependencies tree,
However, Serilog library itself is the base (core) library and can be used on other types of projects like console applications, class libraries or Windows forms, and it even supports older versions of .NET framework 4.x, and .NET Standard.
Serilog for ASP.NET Core NuGet has thus far 200M downloads, and Serilog itself has over 741M downloads.
Alternatives to Serilog can be Nlog, Log4Net and OpenTelemtry.
Microsoft.EntityFrameworkCore
This is the .NET Core version of the widely popular and great ORM library - Entity Framework.
With both, you can implement a full mapping with a relational database and configure the data/structure migrations.
EF and EF Core provide you with different approaches to build your database, which are design-first or code-first.
Check my tutorial Build RESTful APIs Using ASP.NET Core and Entity Framework Core to learn how to build Web APIs that connect to SQL Server Database using the EF Core in .NET 6.
NuGet Downloads for EF Core: 679M
A head-to-head rival for EF Core is Dapper, which is a lightweight, superfast and equally powerful ORM library.
FluentValidation.AspNetCore
Fluent validation API has been there for quite a long time now and here we are talking about the extensions provided by Fluent validation to ASP.NET Core
This NuGet library allows to add validations (manual or automatic) on ASP.NET Core Web API Controllers.
With Fluent Validation you can easily apply model validations through explicit fluent API code to chain multiple validations along with the ability to provide customized validation for your complex types.
NuGet Downloads for FluentValidation for ASP.NET Core: 94.6M
Mapster
Mapster has been doing amazingly great in terms of performance versus its widely popular rival (AutoMapper) to provide a mapping tool between your entity or domain-level POCOs and your DTOs or models.
Instead of manually mapping every field, you can rely on the Mapster to bind the matching entity with the DTO, while leaving you the ability to explicitly define the mapping configuration for the unmatching fields.
In addition to the performance benefit over AutoMapper, Mapster is quite simple to use and can seamlessly integrate with your ASP.NET Core Web API project.
To learn more about Mapster you can check its official GitHub repo.
Mapster NuGet Package currently has more than 13M downloads.
Microsoft.AspNetCore.Mvc.Versioning
Using the ASP.NET Core MVC Versioning, you can easily enable different types of versioning of your routes and endpoints.
Versioning is very important to allow backward combability for older clients while you are applying breaking changes so you can have multiple versions of your endpoints, and you can also deprecate the older ones and allow a grace period for your clients to migrate to the new APIs.
Check this great tutorial made by CodeMaze team to help you implement Versioning in ASP.NET Core Web API.
You should start implementing API versioning from day 1 of building your API project.
This NuGet has already surpassed the 100M downloads from the NuGet source.
Refit
If you've ever worked on Android or Java, you will realize that Refit is the Retrofit of .NET Core.
In fact, the concept has been inspired by Retrofit Library.
Built on the HTTPClient, Refit enables you to manifest your REST API usage into Interfaces, thus making it easier to organize your RESTful calls, abstract your
Refit 6 is the latest version and it is actively maintained by the community through its GitHub repo.
Refit is not only beneficial for an ASP.NET Core project but it can also be used in any .NET project, .NET Core or Xamarin.
With over 48M NuGet Package downloads, Refit is a robust and fast-growing library that is simplifying the way .NET applications can call different RESTful APIs and reducing extra efforts on developers so that they can focus on building quality applications.
StackExchange.Redis
The best way to implement distributed caching is to use Redis, Redis is short for Remote Dictionary Server, which defines a store that can contain different data structures like strings, lists, dictionaries, maps and others.
At its core, It was built to fix scalability issues where it allows reliable and consistent storage across multiple servers.
Redis also provides both synchronous and asynchronous operations with its data store, as well as streaming and messaging functions that can be a solid implementation for a real-time application.
Check this tutorial to learn more.
Download the Redis NuGet Package and join 251M developers to start using its great distributed caching features.
In ASP.NET Core you can use Data Protection APIs to secure store API Keys and implement API Key Rotation, and using the NuGet Redis extension for ASP.NET Core and Redis (Microsoft.AspNetCore.DataProtection.StackExchangeRedis) can also be used to persist data protection keys.
To read more about configuring Data protection in ASP.NET Core you can read the official docs.
MediatR
Easier than before, MediatR can help you decouple your application components by introducing request/response, handlers, and events with the ability to split between command and query.
MediatR helps you implement the Mediator design pattern across your .NET projects to enable vertical layering, and loose coupling and improve the communication between the different classes and components.
Check the following article to learn more about the Mediator Design Pattern.
Having the above can help you achieve a clean architecture solution with the proper vertical slicing and loose coupling of components.
With a single line of configuration, you can inject your assembly, such as your "Core.Application project" into your ASP.NET Core Web API pipeline by calling the AddMediatR extension method.
And then you will have clean controllers by using Send method of the injected MediatR instance through the IMediatR interface in your ASP.NET Core Web API project
This is only one of the capabilities provided by the MediatR library and its extensions in ASP.NET Core.
MediatR NuGet so far has over 117M downloads.
xunit
xUnit.net is a testing library and tool that helps you build robust tests to support your test-driven development approach, xUnit.net relies on 2 concepts to build any test method: Facts and Theories
Fact is when you expect your test to always return true
Theory is when you want to experiment with different data to assert different test results using a single unit of test. This is mainly helpful for data-driven testing that includes heavily testing a method for different inputs without having to rewrite and duplicate your unit tests.
xUnit also supports running tests on several .NET frameworks at a time to allow you determine if your code can be supported in the different versions of .NET ( old and new ).
By the time of writing this tutorial, xUnit NuGet has been downloaded 312M times, and still counting with more and more developers writing tests and adopting the Test-Driven Development approach.
Moq
Moq is one of the best libraries that provide Mocking features for your models which can ultimately help you build solid tests.
Instead of having to write your own fake or mock class, Moq handles that for you nicely and neatly.
With the unique feature of Linq to Mocks, you can seamlessly write Linq queries and lambda expressions that would mock a certain behaviour.
Moq was mainly built to support the Test-Driven Development approach but can be used in any use case where mocking features are needed.
With over 427M downloads, Moq is the most popular and friendly mocking framework for .NET.
Alternatives to Moq include JustMock.
Summary
In this tutorial, we overviewed 13 libraries in ASP.NET Core every developer should know about.
These libraries can literally save hundreds of hours of development efforts and help you and your team boost and enhance the development of ASP.NET Core applications.
Why reinvent the wheel and write your own methods or even libraries to achieve common functions? While there are world-class open-source libraries that are continuously maintained and supported by community experts.
These production-ready libraries are there for you to try them out and even you can contribute to them.
To learn more about ASP.NET Core, check my tutorials below :
Bonus
Music can enhance your learning by boosting your spirit and releasing you from stress, which will imply a better learning experience and outcome.
In today's bonus music, I am sharing with you a collection of great baroque keyboard pieces that were composed and arranged by great baroque musicians including J.S. Bach and a few of his family musicians and their close friends.
These pieces, including keyboard works and vocals, were added as part of a notebook that was presented by J.S. Bach to his 2nd wife - Anna Magdalena Bach (Soprano Singer) in the year 1725.
I hope you enjoy these brilliant keyboard pieces from Anna Magdalena Bach's Notebook played by Paul Barton using his FEURICH harmonic pedal piano.
Happy coding and listening.
https://www.youtube.com/watch?v=FRyE7kN0rlk
This article originally appears in https://www.codingsonata.com/13-libraries-in-asp-net-core-every-developer-should-know-about/