dir.by  
  Search  
Programming, development, testing
Windows Service (using C#)
How to register your exe file as Windows Service. Start/stop your Windows Service on the system Windows | library .NET Core using Worker
  Looked at 2295 times    
 Your Windows Service on the system Windows | library .NET Core using Worker 
last updated: 16 April 2023
Step 1. Register your exe file as Windows Service
Run Windows PowerShell
 
Register the exe file as a service.
To do this, execute the command:
sc.exe create "My Worker Service Core1" binPath="D:\MyWorkerServiceCore1\publish\MyWorkerServiceCore1.exe"
The name of the service.
You can write any name.
My Worker Service Core1
Path to your program
D:\MyWorkerServiceCore1\publish\MyWorkerServiceCore1.exe
 
Open Services and see that the service is installed in the system Windows:
Step 2. Run your Windows Service in the system Windows
 
We can see that my service My Worker Service Core1 has started:
Step 3. Open Event Viewer and see that my service writes information in Event Viewer every 1 second
 
My service writes information in Event Viewer every 1 second because here's the code:
  File Worker.cs
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace MyWorkerServiceCore1
{
     public class Worker : BackgroundService
     {
          private readonly ILogger<Worker> _logger;

          public Worker(ILogger<Worker> logger)
          {
               _logger = logger;
          }

          protected override async Task ExecuteAsync(CancellationToken stoppingToken)
          {
               while (!stoppingToken.IsCancellationRequested)
               {
                    _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
                    await Task.Delay(1000, stoppingToken);
               }
          }
     }
}
Step 4. How to remove your Windows Service from the system Windows?
 
Execute the command:
sc.exe delete "My Worker Service Core1"
 
Now my service is not in the system:
 
← Previous topic
Doing publish a project (compiling and assembling a project) "C# Windows Service" | library .NET Core using Worker
 
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
Technology .NET Framework
Create a new application C# Windows Service (.NET Framework)
C# Windows Service (.NET Framework) with the username. Event when user logs in/out of Windows
Create folder "My Application" under folder "Application and Services logs" | Event Viewer
Technology .NET Core
Create a new application C# Windows Service (library .NET Core and use Worker)
Loading parameters from appsettings.json for application C# Windows Service (library .NET Core and using Worker)
How to open a Notepad app from C# Windows Service | library .NET Core and use Worker
Doing publish a project (compiling and assembling a project) "C# Windows Service" | library .NET Core using Worker
How to register your exe file as Windows Service. Start/stop your Windows Service on the system Windows | library .NET Core using Worker

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика