How to create windows service
Video Link
Step 1. Create a new project and use Windows service project as template
Click on Create. You will get below structure
Step 2. Adding Installers to the Service
Before you can run a Windows Service, you need to install the Installer, which registers it with the Service Control Manager.
Go
to the view code of installer
Select the InitializeComponent method and press the F12 key to go the definition.
Change
some settings on it
this.serviceProcessInstaller1.Account
= System.ServiceProcess.ServiceAccount.LocalSystem;
//
this.serviceInstaller1.Description
= "My First Service demo";
this.serviceInstaller1.DisplayName
= "MyFirstService.Demo";
this.serviceInstaller1.ServiceName
= "Service1";
ProjectInstaller.cs
namespace DummyWindowService
{
partial class ProjectInstaller
{
///<summary>
/// Required
designer variable.
///</summary>
private System.ComponentModel.IContainer
components = null;
///<summary>
/// Clean up any
resources being used.
///</summary>
///<param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool
disposing)
{
if (disposing
&& (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region
Component Designer generated code
///<summary>
/// Required method
for Designer support - do not modify
/// the contents of
this method with the code editor.
///</summary>
private void InitializeComponent()
{
this.serviceProcessInstaller1
= newSystem.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1
= newSystem.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account
= System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password
= null;
this.serviceProcessInstaller1.Username
= null;
//
// serviceInstaller1
//
this.serviceInstaller1.Description
= "My First Service demo";
this.serviceInstaller1.DisplayName
= "MyFirstService.Demo";
this.serviceInstaller1.ServiceName
= "Service1";
//
// ProjectInstaller
//
this.Installers.AddRange(newSystem.Configuration.Install.Installer[]
{
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
#endregion
private System.ServiceProcess.ServiceProcessInstaller
serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller
serviceInstaller1;
}
}
Write code for doing task,
Service1.cs
Full
code to write something
Service1.cs
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Diagnostics;
using System.IO;
usingSystem.Linq;
usingSystem.ServiceProcess;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Timers;
namespace DummyWindowService
{
public partial class Service1 :ServiceBase
{
Timer
timer = newTimer();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[]
args)
{
// using (new Impersonator("kumarp",
"cabi_uk", "2738418!"))
{
WriteToFile("Service
is started at " + DateTime.Now);
timer.Elapsed += newElapsedEventHandler(OnElapsedTime);
timer.Interval = 5000; //number in milisecinds
timer.Enabled = true;
}
}
protected override void OnStop()
{
WriteToFile("Service
is stopped at " + DateTime.Now);
}
privatevoidOnElapsedTime(object source,
ElapsedEventArgs e)
{
using (newImpersonator("kumarp", "cabi_uk", "2738"))
{
WriteToFile("Service
is recall at " + DateTime.Now);
}
}
public void WriteToFile(string Message)
{
string path =
AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if(!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath =
AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if(!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWritersw
= File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWritersw
= File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
}
}
How
to install or run it.
Rebuild Your
Application
Search “command Prompt” and run the program as
an administrator:
Fire the below command in the command
prompt and press Enter.
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
It
will contain InstallUtil.exe for installing the service.
services.msc
In
order to install it use
InstallUtil.exe
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe
To
uninstall it
InstallUtil.exe
-u C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe
-u=
used for uninstall the service.
What
looks in command prompt at time of installation
C:\WINDOWS\system32>cd
C:\Windows\Microsoft.NET\Framework\v4.0.30319
C:\Windows\Microsoft.NET\Framework\v4.0.30319>InstallUtil.exe
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe
Microsoft
(R) .NET Framework Installation utility Version 4.8.3752.0
Copyright
(C) Microsoft Corporation. All rights
reserved.
Running
a transacted installation.
Beginning
the Install phase of the installation.
See
the contents of the log file for the
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe
assembly's progress.
The
file is located at C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.InstallLog.
Installing
assembly
'C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe'.
Affected
parameters are:
logtoconsole
=
logfile =
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.InstallLog
assemblypath
=
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe
Installing
service Service1...
Service
Service1 has been successfully installed.
Creating
EventLog source Service1 in log Application...
The
Install phase completed successfully, and the Commit phase is beginning.
See
the contents of the log file for the C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe
assembly's progress.
The
file is located at
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.InstallLog.
Committing
assembly
'C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe'.
Affected
parameters are:
logtoconsole
=
logfile =
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.InstallLog
assemblypath
=
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe
The
Commit phase completed successfully.
The
transacted install has completed.
Uninstall
console output
C:\Windows\Microsoft.NET\Framework\v4.0.30319>InstallUtil.exe
-u
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe
Microsoft
(R) .NET Framework Installation utility Version 4.8.3752.0
Copyright
(C) Microsoft Corporation. All rights
reserved.
The
uninstall is beginning.
See
the contents of the log file for the
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe
assembly's progress.
The
file is located at C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.InstallLog.
Uninstalling
assembly
'C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe'.
Affected
parameters are:
logtoconsole
=
logfile =
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.InstallLog
assemblypath
=
C:\Users\kumarp\source\repos\DummyWindowService\DummyWindowService\bin\Debug\DummyWindowService.exe
Removing
EventLog source Service1.
Service
Service1 is being removed from the system...
Service
Service1 was successfully removed from the system.
The
uninstall has completed.
C:\Windows\Microsoft.NET\Framework\v4.0.30319>
How to see whether service is installed or not. Open services by following the below steps:
- Press Window key + R.
- Type services.msc
- Find your Service.
In
order to start we need to select the service and click on start button.
In
order to start we need to select the service and click on start button.
Service
actual task. It
create a log file
0 comments:
Post a Comment