How to build lightweight services in ASP.NET Core 6 – InfoWorld

By , Columnist, InfoWorld |
When working in web applications in ASP.NET Core, you might often want to build services that are lightweight — i.e., services that don’t have a template or controller class — for lower resource consumption and improved performance. You can create these lightweight services or APIs in the Startup or the Program class.
This article talks about how you can build such lightweight services in ASP.NET Core 6. To work with the code examples provided in this article, you should have Visual Studio 2022 installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 here.
First off, let’s create an ASP.NET Core project in Visual Studio 2022. Following these steps will create a new ASP.NET Core Web API 6 project in Visual Studio 2022:
We’ll use this new ASP.NET Core 6 Web API project to illustrate working with lightweight services in the subsequent sections of this article.
As we’ll be building lightweight services that don’t require a controller, you should now delete the Controllers solution folder and any model classes that are created by default.
Next, open the launchSettings.json file under the Properties solution folder and remove or comment out the launchUrl key-value pair as shown in the code snippet given below.
The launchUrl refers to the host of your application. When the application is started, the URL specified in launchURL is used to start the application. If the URL is wrong or does not exist, the application will throw an error at startup. By removing launchUrl or commenting it out, we ensure that the application doesn’t use the default launchUrl to launch the application and thus we avoid any errors. Once the launchUrl is removed, the application will fall back to port 5000.
We’ll now take advantage of some of the extension methods of the IEndpointConventionBuilder interface to map requests. Here is the list of these extension methods:
The MapGet, MapPost, MapDelete, and MapPut methods are used to connect the request delegate to the routing system. MapRazorPages is used for RazorPages, MapControllers for Controllers, MapHub for SignalR, and MapGrpcService for gRPC.
The following code snippet illustrates how you can use MapGet to create an HTTP Get endpoint.
Now create a new C# file named Author and enter the following code:
Create a read-only list of Author and populate it with some data as shown in the code snippet given below.
You can use the following code to create another endpoint and return the list of Author in JSON format.
To retrieve a particular record based on the Id, you can write the following code:
To add data using an HTTP Post endpoint, you can take advantage of the MapPost extension method as shown below.
To delete data, you can take advantage of the MapDelete extension method as shown below.
Here is the complete source code of the Configure method of the Startup class:
There is another way to create lightweight services in ASP.NET 6. When you create a new ASP.NET Core 6 Empty Project, the Startup.cs file will not be created by default. So, you can write your code to create lightweight services in the Program.cs file. Here is an example that illustrates how you can do this:
Lightweight services or APIs don’t have a template and don’t need a controller class to create them in. You can create such services in the Startup or the Program class.
If you want to implement authorization in the lightweight services demonstrated in this post, you should take advantage of the RequireAuthorization extension method of the IEndpointConventionBuilder interface.
Joydip Kanjilal is a Microsoft MVP in ASP.Net, as well as a speaker and author of several books and articles. He has more than 20 years of experience in IT including more than 16 years in Microsoft .Net and related technologies.
Copyright © 2021 IDG Communications, Inc.
Copyright © 2021 IDG Communications, Inc.