.Net 6 Minimal APIs for Rapid Microservices Development With Low Ceremonies

Ajith Ramawickrama
Ascentic Technology
6 min readJul 29, 2021
Photo by Tim Carey on Unsplash

Microsoft says that .Net 6, the revolutionary version of the .Net will be released on 9th November 2021 at .Net Conf 2021. Currently, the game-changer is still under preview and at the time of writing this post, preview 6 has been released to the public. Microsoft has added significant enhancement to preview 6 that makes .Net 6 even stronger. Following are the some of new features that already have shipped with .Net 6.

o Simplified and less code with C# 10

o .Net Multi-Platform App UI (MAUI) for native mobile and desktop apps.

o Blazor desktop web app with native device capabilities.

o Minimal Web APIs for microservices.

o More device targets including Apple M1

o Windows Forms and WPF on ARM64 and Single file EXE deployment

o Developer productivity enhancements. Ex: Hot reload

o Performance improvement in runtime, build time, and Entity Framework Core

If you have not upgraded your system to .Net 6, This article would help you to do so.

The source code of this post can be found in this GitHub repository.

I this post, I am going to discuss one of the coolest features from the above list. That is Minimal APIs. With minimal APIs, you can write REST APIs with few lines of code. No startup.cs, no controllers, Just 4 lines of code to write an API endpoint. It is amazing!! isn’t it? Let's see how to do this.

Open your command prompt/terminal from the directory you like and enter the following command.

Then open the directory from your favorite editor and check the program.cs.

var builder = WebApplication.CreateBuilder(args);var app = builder.Build();app.MapGet("/", (() => "Hello World!"));app.Run();

Yes, that’s all. You have just written a REST API endpoint with just four lines of code without having startup.cs or controller classes. It is just mapping a route with an action. If you are coming from Node JS background, you can see that this is very similar to REST APIs in Express JS.

You can test your API with dotnet run command.

However, this API does not make any sense due to it is just a simple endpoint that returns a string. To build a comprehensive API, you need to add authentication, CORS, Open API integration, data access …etc. Let's add some functionalities to this API.

Middleware Support

As you can see, builder.Services return IServiceCollection interface. Therefore, you can register any service same as you do in .Net Core API’s startup.cs.

Configuring CORS

In order to register CORS, you have to install Microsoft.Extensions.DependencyInjection nuget package.

Open API (Swagger) Support

Open API support for minimal API has been shipped with preview 6. If you haven't updated your .Net runtime to 6.0.0-preview.6.21352.12, this feature will not work.

Run the application and navigate to http://localhost:5000/swagger

Entity Framework Support

As usual, minimal APIs support EntityFramework core and all the data providers. However, migrations command in code-first approach does not support in .Net 6 Preview 5. If you are using preview 5, still you can update your database as explained by David Fowler in his Github repo.

Registering EntityFramework

Update the database

You can update the database against your code-first DbContext by adding the above code. However, I upgraded my system to .Net 6 preview 6 and upgraded my entity framework core from EntityFrameworkCore 5 to EntityFrameworkCore 6 preview. Now migration commands are working as expected.

Remove already installed Entity Framework Tools

dotnet tool uninstall --global dotnet-ef

Install the latest Preview

dotnet add package Microsoft.EntityFrameworkCore.Tools --version 6.0.0-preview.6.21352.1

Then add the following packages to your project.

dotnet add package Microsoft.EntityFrameworkCore --version 6.0.0-preview.6.21352.1dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 6.0.0-preview.6.21352.1dotnet add package Microsoft.EntityFrameworkCore.Tools --version 6.0.0-preview.6.21352.1

Now all the migration commands should be working properly.

Dependency Injection

Have a closer look at the program.cs located in the Github repo.

As you can see in the underlined code, the parameter dbcontext has been decorated with [FromService] annotation which indicates that EmployeeDbContext has been injected from the service registration. This is how you injecting services into REST API action methods in minimal APIs.

Authentication/Authorization Support

Minimal APIs support all the standard authentication methods such as JWT Bearer, Cookie Authentication, OAUTH2, Azure AD Authentication….ect. In this example, I have implemented JWT Bearer authentication.

Registering Authorization and Authentication services.

Using authentication/authorization middlewares.

Generate JWT Tokens

Authenticate Users.

Securing Endpoints.

C# Global Usings

In addition to .Net 6 minimal APIs, I have used another cool feature that comes with C# 10. As you can see in the source code, none of the classes are using the “using” keyword to reference namespaces. Instead, I have created. Usings.cs file to define all the namespace references that can use anywhere in the project classes without using them again at the top of each class. To work this properly, you have to use the “global” keyword wherever you define your global namespaces. There is no strict name for this file. You can use any name for this file.

Summary

In minimal API, It maps an http route with an action method. .Net minimal APIs are a greater option to develop small microservices which serve few endpoints. Due to its low ceremony but still powerful features helps to build microservices faster and makes it easy to maintain the source code. It supports most of the features provide by .Net Core Web APIs, and Microsoft adding features into it continuously. In spite of minimal APIs provide powerful features, it is recommended to use .Net Core Web API, if your API has hundreds of endpoints.

The source code of this post can be found in this GitHub repository

--

--