Skip to content

mekkl/dotnet-starter-template

Repository files navigation

Build Quality Gate Status CodeQL Coverage

Introduction

Starter dotnet project template with:

  • ...
  • ...

Table of Content

Getting Started

Setting up database for development

This solution is utilizing Docker container with a SQL server image when running the server locally.

First install Docker Desktop from https://www.docker.com/products/docker-desktop/

When installed open the commandline and execute the following to install the latest SQL server image:

docker pull mcr.microsoft.com/mssql/server:2022-latest

To start the container, use the following docker command:

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=yourStrong(!)Password" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2022-latest

Afterwards the container can be started/stopped via. the "play" botton in the Containers section via. Docker Desktop.

alt text

Ensure that this setting is found in the appsettings.*.json file(s).

{
      "Azure": {
            "SqlConnectionString": "Data Source=localhost,1433; Persist Security Info=True;User ID=SA;Password=yourStrong(!)Password; TrustServerCertificate=True"
      }
}

Startup (without Docker)

First download and install the .NET Core SDK here.

You can check to see if you have installed the .Net Core SDK version (currently using the latest 6.0) with this command $ dotnet --version

To startup the API open up a console, at the project root.

Firstly go to the Presentation.WebApi folder:

cd Presentation.WebApi

Then start the API application with the following command:

dotnet run

The output from the command should display which url and port the API is listining on:

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Users\ML\Desktop\portfolio-web\web-api

Startup (with Docker)

TODO: Setup docker network between app and db container

First download and install Docker from here.

A Microsoft guide can also be found here

Create the image:

docker build -t <IMAGE_NAME> -f ./MinimalApi/Dockerfile .

Create the container

docker create --name <CONTAINER_NAME> -p 8081:80 -p 8082:443 <IMAGE_NAME>

The output of the create command will produce a CONTAINER ID like:

7d69d4022efd7eb5190c0156e6c41f2b5d0fead44c50c3e29d4056bfc6c65bf1

You can list all containers with:

docker ps -a

Starting a container:

docker start <CONTAINER_NAME>

Or start from image:

docker run --name <CONTAINER_NAME> -p 8081:80 -p 8082:443 -d <IMAGE_NAME>

Stopping a container:

docker stop <CONTAINER_NAME>

(TODO) Docker with HTTPS

To be worked on...

Docker with compose

Some notes on used composed setup.

  • When using Docker compose as is, the environment is set to production. Keep this in mind for flows that depends on specific environments
  • The connection string, with this setup, needs to have the localhost replaced with the service name of the database name in the compose.yml file. The production connection string therefore contains db instead of localhost

Start compose:

docker compose up -d   

List containers:

docker ps

Shutdown:

docker compose down

Build and Test

Command for building the project (run the command at the project root):

dotnet build

Command for running all tests (run the command at the project root):

dotnet test

Command for running all tests, and generating coverage data. The result is ending op in each test project in the dir Tests/TestResults/<GUID>/coverage.cobertura.xml

dotnet test --collect:"XPlat Code Coverage" --settings Tests/coverlet.runsettings

Command for generating coverage report from results:

reportgenerator "-reports:*/TestResults/**/coverage.info" "-targetdir:Tests/CoverageReports" -reporttypes:html

Usage of reportgenerator see: https://github.com/danielpalme/ReportGenerator

Entity Framework

Link: https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=vs

Microsoft.EntityFrameworkCore.Design This package is required for the Entity Framework Core Tools to work.

Microsoft.EntityFrameworkCore.Tools nuget enables Package Manager Console commands

When using the Package Manager Console, set default Project to Infrastructure

Add migration

add-migration <migration-name> -OutputDir Your\Directory 
dotnet ef migrations add InitialCreate --project .\Infrastructure\ --startup-project .\MinimalApi\

Apply migration

update-database
dotnet ef database update --project .\Infrastructure\ --startup-project .\MinimalApi\

Apply specific migration (rollback to previous)

Update-Database <previous-migration-name>
dotnet ef database update <previous-migration-name> --project .\Infrastructure\ --startup-project .\MinimalApi\

Remove migration (latest migration)

> Remove-Migration
dotnet ef migrations remove --project .\Infrastructure\ --startup-project .\MinimalApi\

List migrations

Get-Migration
dotnet ef migrations list --project .\Infrastructure\ --startup-project .\MinimalApi\

Nuget versions update

When having this project in GitHub the Dependabot can be used to automatically create pull requests when new releases of dependencies is available. See more here

A tool for checking dependencies locally can also be used. In these examples is dotnet-outdated utilized. See more here.

First install the tool:

dotnet tool install --global dotnet-outdated-tool

Or if the tool is already installed, it can be updated with:

dotnet tool update --global dotnet-outdated-tool

Command for checking nuget version

dotnet outdated

Command for checking nuget version and upgrade

dotnet outdated -u

Backend Resources/Inspirations

Benchmark testing

Load testing

Versioning

JSON Serialize

SignalR

Test resources

Enforce HTTPS

E2E (End-to-end) testing

Fluent Validation Doc

Codecov (coverage report)

CodeQL

Endpoints

Frontend Resources/Inspirations

Flexbox demo

Releases

No releases published

Packages

No packages published

Languages