Getting Started with Microservices in ASP.NET Core: Step-by-Step Guide

Microservices are a modern software architecture pattern that involves breaking down a monolithic application into smaller, independent services that can be deployed, managed, and scaled separately. This approach provides several benefits, including better scalability, faster development, and improved fault tolerance. In this blog, we will learn how to create a simple "Hello World" microservice using ASP.NET Core.

Prerequisites

  • Visual Studio 2019
  • .NET Core 3.1 SDK

Step 1. Creating a new ASP.NET Core Web API project

To create a new ASP.NET Core Web API project in Visual Studio 2019, follow these steps:

  1. Open Visual Studio 2019 and click on "Create a new project".

  2. In the "Create a new project" window, choose "ASP.NET Core Web API" from the list of templates and click the "Next" button.

  3. Enter a name for the project, such as "HelloWorldMicroservice", and choose a location for the solution.

  4. In the "Create a new project" window, select "API" and click the "Create" button.

Step 2. Adding a new Controller

Now that we have created a new ASP.NET Core Web API project, let's add a new Controller to handle the "Hello World" request.

  1. In the "Solution Explorer" window, right-click on the "Controllers" folder and choose "Add" -> "Controller".

  2. In the "Add Scaffold" window, choose "API Controller with actions, using Entity Framework" and click the "Add" button.

  3. Enter a name for the controller, such as "HelloWorldController", and click the "Add" button.

Step 3. Implementing the Hello World API

With the new Controller in place, let's implement the "Hello World" API.

  1. Open the HelloWorldController.cs file and replace its contents with the following code:
    using Microsoft.AspNetCore.Mvc;
    
    namespace HelloWorldMicroservice.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        public class HelloWorldController : ControllerBase
        {
            [HttpGet]
            public string Get()
            {
                return "Hello World";
            }
        }
    }
    

    In this code, we have defined a single "Get" action that returns the "Hello World" message. The [Route("api/[controller]")] attribute defines the base URL for the API, and the [ApiController] attribute enables automatic model validation and error handling.

Step 4. Testing the Hello World API

With the Hello World API implemented, let's test it.

  1. Press the "F5" key or click on the "Debug" menu and choose "Start Debugging".

  2. In a web browser, navigate to "https://localhost:5001/api/helloworld".

You should see the "Hello World" message displayed in the browser.

Step 5. Deploying to Docker

Finally, let's deploy our Hello World microservice to Docker.

  1. Right-click on the solution and choose "Add" -> "Docker Support".

  2. In the "Add Docker Support" window, choose "Linux" and click the "Add" button.

  3. Open the Dockerfile and replace its contents with the following code:

    FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base
    WORKDIR /app
    EXPOSE 80
    
    FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
    WORKDIR /src
    COPY ["HelloWorldMicroservice.csproj", ""]
    RUN dotnet restore "HelloWorldMicroservice.csproj"
    COPY . .
    WORKDIR "/src/."
    RUN dotnet build "HelloWorldMicroservice.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "HelloWorldMicroservice.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "HelloWorldMicroservice.dll"]
    

    In this Dockerfile, we are using the ASP.NET Core 3.1 base image, exposing port 80, and copying the published application to the final image. We are also setting the entry point for the container to be the HelloWorldMicroservice.dll file.

  4. Open a command prompt or terminal window and navigate to the solution folder.

  5. Run the following command to build the Docker image:

    docker build -t helloworldmicroservice .
    
  6. Run the following command to run the Docker container:
    docker run -p 80:80 helloworldmicroservice
    
  7. In a web browser, navigate to "http://localhost/api/helloworld".

You should see the "Hello World" message displayed in the browser.

Congratulations! You have successfully created a "Hello World" microservice using ASP.NET Core and deployed it to Docker. With this foundation, you can start building more complex microservices, incorporating additional features such as authentication, authorization, and database access.