Post

Epoch 1: Building Project

Mermaid demo

Epoch 1: Building Project

Epoch 1 – Create the First Microservice with .NET CLI

Business Story

ABC Company is building a new EShopping platform using a Microservices Architecture.

The Backend team has been assigned to develop the first microservice: Product Service, which is responsible for managing product catalogs and inventory.

The Tech Lead defines the following requirements:

  • Use .NET 8
  • Create the project using ASP.NET Core Web API
  • Initialize everything using the .NET CLI
  • The project must build and run successfully
  • Database integration is not required in this epoch

Learning Objectives

After completing this epoch, students will be able to:

  • Install and verify the .NET SDK
  • Use the .NET CLI
  • Create a Solution
  • Create an ASP.NET Core Web API project
  • Add projects to a Solution
  • Build and run applications
  • Understand the basic structure of an ASP.NET Core project

What is the .NET CLI?

The .NET CLI (Command Line Interface) is the official command-line tool provided by Microsoft for developing .NET applications.

It allows developers to:

  • Create projects
  • Build applications
  • Run applications
  • Publish applications
  • Execute tests
  • Install NuGet packages
  • Generate Entity Framework migrations

In professional software development, projects are often initialized using the .NET CLI instead of Visual Studio.


Step 1. Verify the Installed .NET SDK

Check the installed SDK version.

1
dotnet --version

Example output:

1
8.0.412

List all installed SDKs.

1
dotnet --list-sdks

Step 2. Create the Project Workspace

1
2
mkdir EShopping
cd EShopping

Verify the current working directory.

1
pwd

Step 3. Create the Solution

1
dotnet new sln -n EShopping

Expected output:

1
EShopping.sln

Step 4. Create the Project Structure

1
2
3
4
5
EShopping
├── src
├── tests
├── docs
└── docker

Create the directories.

1
2
3
4
mkdir src
mkdir tests
mkdir docs
mkdir docker

Step 5. Create the Product Service

Move into the src directory.

1
cd src

Create an ASP.NET Core Web API project.

1
dotnet new webapi -n ProductService

The resulting structure:

1
2
src
└── ProductService

Step 6. Return to the Solution Root

1
cd ..

Step 7. Add the Project to the Solution

1
dotnet sln add src/ProductService/ProductService.csproj

Verify the solution.

1
dotnet sln list

Expected output:

1
2
3
Project(s)
----------
src/ProductService/ProductService.csproj

Step 8. Build the Solution

1
dotnet build

Expected output:

1
Build succeeded.

Step 9. Run the Application

Option 1

1
2
3
cd src/ProductService

dotnet run

Option 2

1
dotnet run --project src/ProductService

Example output:

1
2
3
4
5
Now listening on:

http://localhost:5000

https://localhost:5001

Step 10. Open Swagger

Open one of the following URLs in your browser:

1
https://localhost:5001/swagger

or

1
http://localhost:5000/swagger

If the Swagger UI appears successfully, the project has been created correctly.


Step 11. Understand the Project Structure

1
2
3
4
5
6
7
8
ProductService
│
├── Program.cs
├── appsettings.json
├── appsettings.Development.json
├── ProductService.csproj
├── Properties
└── obj
File/FolderDescription
Program.csThe application’s entry point. Configures dependency injection, middleware, and routing.
appsettings.jsonStores application configuration such as logging, connection strings, and other settings.
appsettings.Development.jsonOverrides configuration when running in the Development environment.
ProductService.csprojDefines the project, target framework, package references, and build configuration.
Properties/launchSettings.jsonContains launch profiles for local development.
objAuto-generated build artifacts. Do not modify manually.

Step 12. Enable Hot Reload

Run the application with Hot Reload enabled.

1
dotnet watch

or

1
dotnet watch run

The application will automatically rebuild whenever a source file is saved.


Project Structure After Epoch 1

1
2
3
4
5
6
7
8
9
10
11
12
13
EShopping
│
├── docker
├── docs
├── tests
├── src
│   └── ProductService
│       ├── Program.cs
│       ├── appsettings.json
│       ├── ProductService.csproj
│       └── Properties
│
└── EShopping.sln

Hands-on Exercise

Complete the following tasks:

  1. Create an EShopping solution.
  2. Create the folders:
    • src
    • tests
    • docs
    • docker
  3. Create a ProductService project using the .NET CLI.
  4. Add the project to the solution.
  5. Build the solution successfully.
  6. Run the application.
  7. Open Swagger and verify that the default endpoint is accessible.

Next Epoch Preview

In Epoch 2, we will refactor the generated Web API project into a Clean Architecture by separating it into multiple projects:

  • ProductService.API
  • ProductService.Application
  • ProductService.Domain
  • ProductService.Infrastructure

Students will learn why this layered architecture is widely adopted in enterprise-grade Microservices systems.

This post is licensed under CC BY 4.0 by the author.