004 .NET Launch Flow - CarrieKroutil/Reactivities GitHub Wiki

launchsettings.json

./API/Properties/launchsettings.json - contains the instructions used when dotnet run is ran.

  • The most important point that you need to keep in mind is this launchSettings.json file is only used within the local development machine. That means this file is not required when we publishing our http://asp.net/ core application to the production server.

Basic file example:

{
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": false,
      "launchUrl": "swagger",
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Program.cs

When run, dotnet looks at the Program.cs file first for instructions.

Kestrel Server

First line in there will spin up a new Kestrel server which is cross-platform, compared to windows only IIS.

  • var builder = WebApplication.CreateBuilder(args);

Config files for the server live in API folder - appsettings.Development.json and appsettings.json.

  • The one specified for the environment running takes priority over default.

Services

Services added to builder can be thought of as things we can use inside our code.

  • Recommended to use dependency injection to inject services inside our other classes.
// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

Middleware

Things that can do something with HTTP request on the way in or out in the pipeline.

var app = builder.Build();

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

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

API Controllers

The app.MapControllers(); maps to the ./Controllers folder. Each controller contains routes and endpoints that are registered in middleware so when requests come in, it knows where to send it.

Run App

In terminal, run dotnet run and open up a browser to http://localhost:5000/swagger as that is the only thing being listened to at the moment.