To Create A window Service Just Go To Your Visual Studio Click On New projects Select Windows Option from Left Side and select Window Service from Right side window
When You will Go to Code View You will Find The Code window like
Create A new Class in coding called Window Service which will inherite installer class.
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer
{
public WindowsServiceInstaller()
{
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
//# Service Account Information
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
//# Service Information
serviceInstaller.DisplayName = "Your Service Name";
serviceInstaller.StartType = ServiceStartMode.Automatic;
// This must be identical to the WindowsService.ServiceBase name
// set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = "Your Service Name";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
public class WindowsServiceInstaller : Installer
{
public WindowsServiceInstaller()
{
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
//# Service Account Information
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
//# Service Information
serviceInstaller.DisplayName = "Your Service Name";
serviceInstaller.StartType = ServiceStartMode.Automatic;
// This must be identical to the WindowsService.ServiceBase name
// set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = "Your Service Name";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
Create Your user defined method MyMethod() do your stuff there and call it from OnStart() Method.
Now For Installing the service open VS Command Prompt.and go to path where this service exe file is present.it will be in bin\debug folder.
Write The command for Installing InstallUtil /i yourServiceName.exe
when you will execte this command you will find window like
Now Your Service will be created you can see from service Command from RUN
command for Uninstalling The service InstallUtil /u YourServiceName.exe
No comments:
Post a Comment