Dot Net Api Rest Basic Template
Is a AspNet Core (API Rest) template to provide Best Practices and use several professional componentes and much more. Also everything you have to know how about it and how DotNet Templates work.
Code Quaity | Buid & deploy status for develop | Buid & deploy status for master |
---|---|---|
API project structure:
- Application
- Models. Models, ViewModels and DTOs used by Controllers and Services.
- Services. Application Logic.
- Controllers (Think controllers)
- Domain
- Exceptions.
- Interfaces.
- Infrastructure
- ActionResults
- EntityConfigurations. EF programatically configuration.
- Filters. ASPNet Core Filteres.
- Http. Client to invoque other API Rests.
- Middlewares.
- Providers.
- Repositories. DB repositories to use EF easyly in an isolatted layer.
k8s folder Kubernetes and helm “.YAML” configuration to publish API to Kubernetes.
azure folder Azure resources scripts to register App in Azure Active Directory (AAD). other scripts, like ARM (Azure Resource Manager), could be added here.
What is going to Learn?
- DotNet Templates
- API REST using NetCore and best practices:
- Swagger
- Serilog and also ‘ApplicationInsight’ + Serilog Extensions.
- Entity Framework Core for Commands.
- Automapper. Optional to map Model/entities.
- Entity Framework.
- Health Checks.
- Authentication using Azure Active Directory(AAD App Registration).
- Docker
- Kubernetes and Helm
- Azure Pipelines
- SonarQube / Sonar Cloud.io
- Api Management & Authenticacion AAD [Future Version]
- Azure DevSpaces [Future Version]
- And much more.
Blog
All those new Patterns, Tools and Platforms, will be Blog Post entries in elGuerre.com
Runnng the App
- Using Kestrel from Visual Studio:
cd ./content/src/ElGuerre.Items.Api dotnet run ...
- Using Docker from Visual Studio or Docker Command Line:
cd ./content docker build -t items-api . docker run items-api
- Run Entity Framework Migration:
cd ./content/src/ElGuerre.Items.Api dotnet ef migrations add Init --startup-project ..\src\ElGuerre.Items.Api.csproj dotnet ef database update --startup-project ..\src\ElGuerre.Items.Api.csproj
Generate and publish Template
Package a new template version
Nuget.exe pack ElGuerre.ApiRest.Template.nuspec -Version 1.0.n
Nuget.org
dotnet nuget push AppLogger.1.0.0.nupkg -k xxx -s https://api.nuget.org/v3/index.json
Github (Packages)
Update Nuget.config
- Update
%APPDATA%\NuGet\NuGet.config
and delete properties “github”. - Set or updated Nuget.config properties with the password already got in the previous steps
nuget.exe sources Add -Name github -Source https://nuget.pkg.github.com/juanluelguerre/index.json -username ### USERNAME ### -password ### TOKEN ###
Note TOKEN will be generated as follow. More details here.
Publish a new template version
dotnet nuget push "ElGuerre.ApiRest.Template.1.0.0.nupkg" --source "github"
Publish a new template version
dotnet nuget push --source github ElGuerre.ApiRest.Template.1.0.n.nupkg
ornuget.exe push -Source github ElGuerre.ApiRest.Template.1.0.n.nupkg
Azure DevOps (Artifacts)
Generate a new token
- %USERPROFILE%.nuget\plugins\netfx\CredentialProvider.Microsoft\CredentialProvider.Microsoft.exe -U https://pkgs.dev.azure.com/JuanluElGuerre/elGuerre/_packaging/templates/nuget/v3/index.json
Update Nuget.config
- Update
%APPDATA%\NuGet\NuGet.config
and delete properties “templates”. - Set or updated Nuget.config properties with the password already got in the previous steps
nuget.exe sources Add -Name templates -Source https://pkgs.dev.azure.com/JuanluElGuerre/elGuerre/_packaging/Templates/nuget/v3/index.json -username unused -password ### TOKEN ###
Publish a new template version
- nuget.exe push -source templates -ApiKey AzureDevOps ElGuerre.ApiRest.Template.1.0.n.nupkg
Install and uninstall templates
Clear cache: dotnet nuget locals http-cache --clear
Add new templates dotnet new -i ElGuerre.ApiRest.Template::*
Note: List of templeates cached: dotnet nuget locals all --list
Note: Remove cached of templatess Http and Temps: dotnet nuget locals http-cache --clear
y dotnet nuget locals temp --clear
Azure DevOps Multi-stages
Attach AKS (Azure Kubernetes Services) to ACR (Azure Container Registry)
To authorize AKS to accesos ACR execute the sentence:
az aks update -n elguerreaks -g elguerre --attach-acr elguerreacr.azurecr.io
Navigate to kubectl proxy using
az aks browse --resource-group elguerre --name elguerreaks
Create AKS namespaces ‘dev’, ‘int’ and ‘pro’ :
kubectl apply -f ./k8s/namespace-dev.json
kubectl apply -f ./k8s/namespace-int.json
kubectl apply -f ./k8s/namespace-pro.json
Note: Review azure-pipelines.yml for more detail.