Starter dotnet project template with:
- ...
- ...
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.
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"
}
}
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
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>
To be worked on...
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 thecompose.yml
file. The production connection string therefore containsdb
instead oflocalhost
Start compose:
docker compose up -d
List containers:
docker ps
Shutdown:
docker compose down
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
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 <migration-name> -OutputDir Your\Directory
dotnet ef migrations add InitialCreate --project .\Infrastructure\ --startup-project .\MinimalApi\
update-database
dotnet ef database update --project .\Infrastructure\ --startup-project .\MinimalApi\
Update-Database <previous-migration-name>
dotnet ef database update <previous-migration-name> --project .\Infrastructure\ --startup-project .\MinimalApi\
> Remove-Migration
dotnet ef migrations remove --project .\Infrastructure\ --startup-project .\MinimalApi\
Get-Migration
dotnet ef migrations list --project .\Infrastructure\ --startup-project .\MinimalApi\
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
- https://medium.com/fiverr-engineering/major-minor-patch-a5298e2e1798
- https://www.youtube.com/watch?v=iVHtKG0eU_s
- https://dev.to/htissink/asp-net-core-api-path-versioning-197o
-
https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-code-coverage?tabs=windows
-
Install ReportGenerator for DevOps for usage in pipeline: https://www.jamescroft.co.uk/combining-multiple-code-coverage-results-in-azure-devops/
-
https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md
-
https://docs.specflow.org/projects/specflow/en/latest/vscode/test-execution.html
-
Maybe see for multiple test projects: https://jpenniman.blogspot.com/2019/10/code-coverage-for-multiple-projects-in.html