Skip to content

Latest commit

 

History

History

worker

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Home page of sample projects

Useful links

Useful commands

Create the project

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

Test the worker

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

Manage the window service (using PS ISE)

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 ...

Add user secret on console application

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
  }
}

Has one AppSttings by environment

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)

Todo

  • specific folder in Event Viewer
  • unit tests