Creating a self-hosted Web API with .NET 6: Using Extension Methods for Windows and Linux Deployment

Creating a self-hosted Web API with .NET 6: Using Extension Methods for Windows and Linux Deployment

In this article, we will explore how to create a self-hosted web API using .NET 6 and the extension method. This approach offers more control over the hosting environment compared to the traditional IIS hosting. It also provides a flexible solution for deploying the web API on different platforms, including Windows and Linux.

First, we need to create a new .NET 6 project and add the necessary dependencies, such as the Microsoft.AspNetCore.Hosting and Microsoft.AspNetCore.Mvc packages. These packages provide the foundation for building a web API and handling HTTP requests and responses.

Next, we will implement the web API endpoint using the ASP.NET Core MVC framework. For example, we can create a simple "Hello World" endpoint like this:

[Route("api/[controller]")]
[ApiController]
public class HelloWorldController : ControllerBase
{
    [HttpGet]
    public ActionResult<string> Get()
    {
        return "Hello World";
    }
}        

To host the web API, we will use the CreateHostBuilder method, which allows us to configure the hosting environment and set the startup class for the application.

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });        

Now, we can deploy the web API as a service on Windows using the UseWindowsService extension method of the Microsoft.AspNetCore.Hosting.WindowsServices NuGet package. To use this extension method, we simply need to add the following line of code in the CreateHostBuilder method:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseWindowsService()
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });        

Alternatively, we can deploy the web API as a daemon on Linux using the UseSystemd extension method of the Microsoft.Extensions.Hosting.Systemd NuGet package. To use this extension method, we simply need to add the following line of code in the CreateHostBuilder method:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseSystemd()
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });        

And, if you want to deploy the web API as a service on Windows and as a daemon on Linux, you can simply add both extension methods:

Host.CreateDefaultBuilder(args
            .UseWindowsService()
            .UseSystemd()
            .ConfigureWebHostDefaults(webBuilder => {
              webBuilder.UseStartup<Startup>();
            });)        

You can install a service on Windows using the simple sc (Service Control) utility, and then start it:

sc create MyService binPath= "C:\MyService\MyService.exe"

net start MyService        

or you can stop and uninstall a service using:

net stop MyService

sc delete MyService        

To start a .NET Core daemon as a Systemd service, you will need to create a Systemd unit file. This file will define the service and how it should be started and managed by Systemd. Here's an example of a Systemd unit file for a .NET Core daemon:

[Unit]
Description=My .NET Core Daemon

[Service]
WorkingDirectory=/var/myapp
ExecStart=/usr/bin/dotnet /var/myapp/MyDaemon.dll
Restart=always
RestartSec=10
SyslogIdentifier=MyDaemon
User=myuser

[Install]
WantedBy=multi-user.target        

After you can reload the Systemd configuration:

sudo systemctl daemon-reload        

Once the Systemd unit file is created, you can start the service by running the following command:

sudo systemctl start mydaemon.service        

If you want the service to start automatically when the system starts, you can enable it by running the following command:

sudo systemctl enable mydaemon.service        

Here we are! We have created our self-hosting web API using .NET 6, deployable both on Windows and Linux!

ramesh chandan

Working for ABG as Tech Lead LTIMindtree Full stack Webapi, React, sql

2y

I think using IHostBuilder we can deploy Console application also. But to deploy them on AWS, we need info

Like
Reply
ramesh chandan

Working for ABG as Tech Lead LTIMindtree Full stack Webapi, React, sql

2y

How to deploy this on AWS environment

Like
Reply

To view or add a comment, sign in

More articles by Sebastiano Gazzola

Insights from the community

Others also viewed

Explore topics