Skip to content

.NET Core Microservices - The Complete Guide [2026] - 2. Solution Setup

Tutor: Bhrugen Patel

Course Link

Section length: 10 lectures

← Previous: 1. Introduction | Course overview | Next: 3. Modifications to Existing API →

Summary

Setting up the solution

Lecture Notes

Create Project

  1. Create a blank solution named Mango.
  2. Add to source control (Use Git Changes dialog Create Git Repository)
  3. Create a new solution folder in the project named FrontEnd. This is where the main MVC project code will be located, to consume all the services.
  4. Create a new solution folder in the project named Services to hold all the services.
  5. In the FrontEnd folder create a new MVC project named Mango.Web. Select .Net 11 and keep the other defaults.
  6. In the Services folder add a new ASP.NET Core Web API project called Mango.CouponAPI which we will use to implement the Coupon API microservice. This should be .Net 11, Authentication type set to None, and Enable OpenAPI support selected. We also want Use Controllers to be selected, we will use minimal APIs in a later service.

Add Scalar UI

We will use Scalar documentation for the CouponAPI.

In NuGet Package Manager for the CouponAPI project, add Scalar.ASPNetCore. To add this we just need to add app.MapScalarApiReference(); (from Scalar.AspNetCore) to the development branch of Program.cs:

using Scalar.AspNetCore;

...

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
    app.MapScalarApiReference();
}

...

We want this running under port 7001, so edit the Properties\launchSettings.json https port to this:

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5107",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "scalar/v1",
      "applicationUrl": "https://localhost:7001;http://localhost:5107",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

We have also set the browser to automatically launch by setting both "launchBrowser": true, and for https added the launchUrl path.

Now you can run this service and loading the URL https://localhost:7001/scalar/ will display the scalar documentation for this service.

Setup Entity Framework

Add a Models folder to the CouponAPI project to hold the models that will be used by Entity Framework.

Add the file couponn.cs to define the Coupon model as follows

using System.ComponentModel.DataAnnotations;

namespace Mango.CouponAPI.Models;

public class Coupon
{
    [Key]
    public int CouponId { get; set; }
    [Required]
    public string CouponCode { get; set; }
    [Required]
    public double DiscountAmount { get; set; }
    public int MinAmount { get; set; }
}

Now add the following NuGet packages to the project:

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools (enables migrations)
  • Microsoft.EntityFrameworkCore.Design

Now we need to add a connection string to appsettings.json:

"ConnectionStrings": {
  "DefaultConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Mango_CouponDb;Persist Security Info=True;User ID=sa;Password=Str0ngPa$$w0rd;MultipleActiveResultSets=True;Encrypt=True;TrustServerCertificate=True;Command Timeout=30"
}

Now add the DB context for EF. Create a new folder Data to the project, and into this create the class file ApplicationDbContext.cs as:

using Mango.CouponAPI.Models;
using Microsoft.EntityFrameworkCore;

namespace Mango.CouponAPI.Data;

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }
    public DbSet<Coupon> Coupons { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Coupon>().HasData(
            new Coupon
            {
                CouponId = 1,
                CouponCode = "10OFF",
                DiscountAmount = 10,
                MinAmount = 20
            },
            new Coupon
            {
                CouponId = 2,
                CouponCode = "20OFF",
                DiscountAmount = 20,
                MinAmount = 50
            }
        );
    }
}

Now we need to register this in Program.cs under the // Add services to the container. section:

// Add services to the container.
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

Create Coupon Table

In the Package Manager Console window, switch to Mango.CouponAPI project, and create the migration by typing:

add-migration AddCouponToDb

Then:

update-database

to apply the migration.

Create Coupon DTO and add ~~AutoMapper~~ Mapster

We don't want to expose the database entities to the main application, so we create focused DTO objects.

In the solution create a new DTOs folder, or just add the class file to the Models folder as CouponDto.cs:

public class CouponDto
{
    public int CouponId { get; set; }
    public string CouponCode { get; set; }
    public double DiscountAmount { get; set; }
    public int MinAmount { get; set; }
}

The course uses Automapper, but this is paid for now so I will be using Mapster instead. Add this to the project via Manage NuGet Packages:

  • Mapster
  • Mapster.DependencyInjection

Note: Now is also a good time to delete WeatherForecast.cs and Controllers\WeatherForecastController.cs.

Breaking off from the couser for a moment, I added a Mappings folder to the project, and in it created a new class:

using Mapster;

namespace Mango.CouponAPI.Mappings;

public class CouponMappingConfig : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.NewConfig<Models.Coupon, DTOs.CouponDto>();
        config.NewConfig<DTOs.CouponDto, Models.Coupon>();
    }
}

then add the following to Program.cs to set the mapping up for dependency injection:

// Configure Mapster
// Get global config instance
var config = TypeAdapterConfig.GlobalSettings;
// Scan assembly to auto-register all IRegister profiles
config.Scan(Assembly.GetExecutingAssembly());
// Add Mapster types to DI container
builder.Services.AddSingleton(config);
builder.Services.AddScoped<IMapper, ServiceMapper>();

I added this directly after the setup for the AddDbContext.

Coupon Endpoints with Entity Framework

We now implement the CRUD controllers. These will be added to the Controllers folder using the API Controller with actions, using Entity Framework template. Give it the following settings:

  • Model Class: Coupon (Mango.CouponAPI.Models)
  • DbContext class: ApplicationDbContext (Mango.CouponAPI.Data)
  • Data provider: SQL Server
  • Controller name CouponAPIController

Note: Normally we would use API Controller - Empty

This should now run and the endpoints be callable from the Scalar UI.


← Previous: 1. Introduction | Course overview | Next: 3. Modifications to Existing API →