- Worker:
- Options and configuration:
- Environment selection:
- https://www.thecodebuzz.com/set-appsettings-json-dynamically-dev-and-release-environments-asp-net-core/
- https://www.thecodebuzz.com/createdefaultbuilder-configuration-management-net-core-and-asp-net-core/
- https://stackoverflow.com/questions/46364293/automatically-set-appsettings-json-for-dev-and-release-environments-in-asp-net-c
- https://stackoverflow.com/questions/37322565/dotnet-run-or-dotnet-watch-with-development-environment-from-command-line/43992754#43992754
- https://www.tektutorialshub.com/asp-net-core/aspnetcore_environment-variable-in-asp-net-core/
- User secret in console app:
mkdir worker
dotnet new sln
dotnet new worker -o Worker
dotnet sln add reference .\Worker\
dotnet add package Microsoft.Extensions.Hosting.WindowsServices
dotnet add package Microsoft.Extensions.Logging
dotnet add package Microsoft.Extensions.Logging.EventLog
dotnet add package Microsoft.Extensions.Configuration.UserSecrets #needed if using user secrets on console
Open Terminal and launch the worker (see profiles in launchSettings.json):
clear; dotnet run --project Worker --launch-profile Worker-Dev
clear; dotnet run --project Worker --launch-profile Worker-Prod
Note: need to run Powershell ISE as administrator to use sc
commands.
sc create ".Net Test Worker" binpath=C:\<path to project>\samples\worker\Worker\bin\Debug\net5.0\publish\Worker.exe
sc start
sc delete
sc stop
Do not forget to publish the project before:
dotnet publish ...
By default the path of user secrets is C:\\Users\\<user>\\AppData\\Roaming\\Microsoft\\UserSecrets\\<secretID>\\secrets.json
.
By launching the command dotnet user-secrets list
, we can have the list of current secrets
Sample of secrets.json file:
{
"FirstBackgroundService": {
"IsEnable": false,
"DelayInSeconds": 3
},
"SecondBackgroundService": {
"IsEnable": false,
"DelayInSeconds": 2
}
}
There are several files to modify to get appsettings.Developpement.json working
- launch.json (used by Visual Code Debug)
- launchSettings.json (used by Visual Studio Debug and commmand line like
dotnet run --project Worker --launch-profile Worker-Dev
) - appsettings(.Developpement).json
- Program.cs (to add the logic getting the right appsettings file)
- specific folder in Event Viewer
- unit tests