« Service et csharp » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
(24 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 1 : | Ligne 1 : | ||
[[Category:CSharp]] | [[Category:CSharp]] | ||
= Links = | |||
* [https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer Create a Windows service app] | |||
= Create the project = | = Create the project = | ||
# File → New → Project | # File → New → Project | ||
Ligne 35 : | Ligne 38 : | ||
</filebox> | </filebox> | ||
= Installer = | = [https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer#add-installers-to-the-service Installer] = | ||
# Right-click on {{boxx|MyService.cs}} → View Designer | # Right-click on {{boxx|MyService.cs}} → View Designer | ||
# Right-click on the background → Add Installer | # Right-click on the background → Add Installer | ||
# A new class {{boxx|ProjectInstaller}} has been added | # A new class {{boxx|ProjectInstaller}} has been added | ||
# Right-click on {{boxx|ProjectInstaller.cs}} → View Designer | # Right-click on {{boxx|ProjectInstaller.cs}} → View Designer | ||
# | # Description = {{boxx|This service manages the ...}} | ||
# DisplayName = {{boxx|My service}} | |||
# StartType = {{boxx|Automatic}} | |||
# Select {{boxx|serviceProcessInstaller1}} → Properties → Account = {{boxx|LocalSystem}} | |||
= [https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer#build-the-service Build the service] = | |||
# Right-click on {{boxx|MyService}} project → Properties | |||
# Application tab → Startup object = {{boxx|MyService.Program}} | |||
= [https://docs.microsoft.com/en-us/dotnet/framework/windows-services/how-to-install-and-uninstall-services Install the service] = | |||
== [https://docs.microsoft.com/en-us/dotnet/framework/windows-services/how-to-install-and-uninstall-services#install-using-installutilexe-utility InstallUtil] == | |||
<kode lang='ps'> | |||
# first deploy your service (MyService.exe) to the folder "C:\Program Files\MyService" | |||
# install MyService | |||
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe "C:\Program Files\MyService\MyService.exe" | |||
# uninstall MyService | |||
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /u "C:\Program Files\MyService\MyService.exe" | |||
</kode> | |||
<filebox fn='MyServiceInstaller.cs'> | |||
[RunInstaller(true)] | |||
public class MyServiceInstaller : Installer | |||
{ | |||
public MyServiceInstaller() | |||
{ | |||
var processInstaller = new ServiceProcessInstaller(); | |||
var serviceInstaller = new ServiceInstaller(); | |||
var assemblyDescription = Assembly.GetExecutingAssembly() | |||
.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false) | |||
.OfType<AssemblyDescriptionAttribute>().FirstOrDefault(); | |||
processInstaller.Account = ServiceAccount.LocalSystem; | |||
serviceInstaller.ServiceName = typeof(MyService).Name; | |||
serviceInstaller.DisplayName = string.Concat(typeof(MyService).Name.Select(x => Char.IsUpper(x) ? $" {x}" : $"{x}")).TrimStart(' '); | |||
serviceInstaller.Description = assemblyDescription?.Description; | |||
serviceInstaller.StartType = ServiceStartMode.Manual; | |||
this.Installers.Add(processInstaller); | |||
this.Installers.Add(serviceInstaller); | |||
} | |||
} | |||
</filebox> | |||
<filebox fn='AssemblyInfo.cs'> | |||
[assembly: AssemblyDescription("My Assembly description.")] | |||
</filebox> | |||
== [https://docs.microsoft.com/en-us/dotnet/framework/windows-services/how-to-install-and-uninstall-services PowerShell] == | |||
Open a powershell prompt with administrative credentials. | |||
<kode lang='ps'> | |||
cd $home\source\repos\MyService\MyService\bin\Debug | |||
New-Service -Name "MyService" -DisplayName "My service" -Description "This service runs ..." -StartupType "Manual" -BinaryPathName MyService.exe | |||
# uninstall, PowerShell 6 or later is required | |||
Remove-Service -Name "MyService" | |||
# after the executable for a service is deleted, the service might still be present in the registry | |||
# remove the entry for the service from the registry | |||
sc.exe delete "MyService" | |||
</kode> | |||
== [https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer#install-the-service Developer Command Prompt for VS] == | |||
Open {{boxx|Developer Command Prompt for VS}} with administrative credentials. | |||
<kode lang='dos'> | |||
cd %UserProfile%\source\repos\MyService\MyService\bin\Debug | |||
installutil MyService.exe | |||
REM %windir%\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe | |||
REM uninstall the service | |||
installutil.exe /u MyService.exe | |||
</kode> | |||
= [https://docs.microsoft.com/en-us/visualstudio/deployment/deploying-applications-services-and-components?view=vs-2022#create-an-installer-package-windows-desktop Create an installer package] = | |||
# Install the {{boxx|Wix Toolset Visual Studio Extension}} |
Dernière version du 27 juin 2022 à 22:29
Links
Create the project
- File → New → Project
- Windows Service (.NET Framework)
- Rename Service1.cs to MyService.cs
- Double-click on MyService.cs to open the designer → Properties → ServiceName = MyService
Code
MyService.cs |
public partial class MyService : ServiceBase { public MyService() { InitializeComponent(); } protected override void OnStart(string[] args) { } protected override void OnStop() { } } |
Program.cs |
static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new MyService() }; ServiceBase.Run(ServicesToRun); } |
Installer
- Right-click on MyService.cs → View Designer
- Right-click on the background → Add Installer
- A new class ProjectInstaller has been added
- Right-click on ProjectInstaller.cs → View Designer
- Description = This service manages the ...
- DisplayName = My service
- StartType = Automatic
- Select serviceProcessInstaller1 → Properties → Account = LocalSystem
Build the service
- Right-click on MyService project → Properties
- Application tab → Startup object = MyService.Program
Install the service
InstallUtil
# first deploy your service (MyService.exe) to the folder "C:\Program Files\MyService" # install MyService C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe "C:\Program Files\MyService\MyService.exe" # uninstall MyService C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /u "C:\Program Files\MyService\MyService.exe" |
MyServiceInstaller.cs |
[RunInstaller(true)] public class MyServiceInstaller : Installer { public MyServiceInstaller() { var processInstaller = new ServiceProcessInstaller(); var serviceInstaller = new ServiceInstaller(); var assemblyDescription = Assembly.GetExecutingAssembly() .GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false) .OfType<AssemblyDescriptionAttribute>().FirstOrDefault(); processInstaller.Account = ServiceAccount.LocalSystem; serviceInstaller.ServiceName = typeof(MyService).Name; serviceInstaller.DisplayName = string.Concat(typeof(MyService).Name.Select(x => Char.IsUpper(x) ? $" {x}" : $"{x}")).TrimStart(' '); serviceInstaller.Description = assemblyDescription?.Description; serviceInstaller.StartType = ServiceStartMode.Manual; this.Installers.Add(processInstaller); this.Installers.Add(serviceInstaller); } } |
AssemblyInfo.cs |
[assembly: AssemblyDescription("My Assembly description.")] |
PowerShell
Open a powershell prompt with administrative credentials.
cd $home\source\repos\MyService\MyService\bin\Debug New-Service -Name "MyService" -DisplayName "My service" -Description "This service runs ..." -StartupType "Manual" -BinaryPathName MyService.exe # uninstall, PowerShell 6 or later is required Remove-Service -Name "MyService" # after the executable for a service is deleted, the service might still be present in the registry # remove the entry for the service from the registry sc.exe delete "MyService" |
Developer Command Prompt for VS
Open Developer Command Prompt for VS with administrative credentials.
cd %UserProfile%\source\repos\MyService\MyService\bin\Debug installutil MyService.exe REM %windir%\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe REM uninstall the service installutil.exe /u MyService.exe |
Create an installer package
- Install the Wix Toolset Visual Studio Extension