.NET Tutorial - Your First Microservice

Create your service

In your command prompt, run the following command to create your app:

In your terminal, run the following command to create your app:

Command prompt
dotnet new webapi -o MyMicroservice --no-https

Then, navigate to the new directory created by the previous command:

Command prompt
cd MyMicroservice

What do these commands mean?

The dotnet command creates a new application of type webapi (that's a REST API endpoint).

  • The -o parameter creates a directory named MyMicroservice where your app is stored.
  • The --no-https flag creates an app that will run without an HTTPS certificate, to keep things simple for deployment.

The cd MyMicroservice command puts you into the newly created app directory.

The generated code

Several files were created in the MyMicroservice directory, to give you a simple service that is ready to run, including the following files:

  • Program.cs is the entry point file and contains all the settings and configuration that are loaded when the app starts and has code for a simple API that returns the weather forecast for the next five days. It also starts the application.
  • MyMycroservice.http is used for testing ASP.NET Core projects.
  • MyMicroservice.csproj defines the version of .NET the app is targeting, what libraries the project references, etc.
  • The launchSettings.json file inside the Properties directory defines different profile settings for the local development environment. A port number ranging between 5000-5300 is automatically assigned at project creation and saved on this file.

The following code shows the contents of the Program.cs file:

Program.cs
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

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

var summaries = new[]
{
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
    var forecast =  Enumerable.Range(1, 5).Select(index =>
        new WeatherForecast
        (
            DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
            Random.Shared.Next(-20, 55),
            summaries[Random.Shared.Next(summaries.Length)]
        ))
        .ToArray();
    return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

Select the Continue button below to go to the next step.

Got an error?

If you receive a message similar to Template "ASP.NET Core Web API" could not be created. Failed to create template. Details: Access to the path 'C:\Windows\System32\MyMicroservice' is denied, change your current directory to one where you have permissions to create a new folder and try to run the command again.

If Windows can't find the SDK when you try to create the project and you are sure you have installed the SDK, your machine might have an issue with the PATH environment variable. See this Stack Overflow post for instructions on how to diagnose and fix this issue.

If you can't resolve the issue you're having, select the I ran into an issue button below to get help fixing the problem.

Continue