Introduction:
Recently, I was hunting about
Windows Service. So I wrote this article. I just explained windows Service and
usage in the .Net Application.
Windows Service:
Windows
Service applications are long-running applications that are ideal for use in
server environments. Services can be automatically started
when the computer is booted. Windows Services are
controlled through the Service Control
Manager where they can be stopped, paused, and started as needed.
Create Windows Service:
Add
Assemblies for Windows Service:
1) System.Configuration.Install
2) System.ServiceProcess.
System.Configuration.Install:
Provides
classes that allow you to write custom installers for your own components.
System.ServiceProcess:
Allow
you to implement, install, and control Windows service applications.
System.ServiceProcess.ServiceBase:
Provides
a base class for a service that will exist as part of a service application.
ServiceBase must be derived from when creating a new service class.
Every
Service should override OnStart() and
OnStop() and also Pause, Shutdown and
Continue.
Step by Step Processing:
· Create New Application.
· Select Windows Service in the Template Window.
· Go to Program.cs file.
· Declare namespace Using System.ServiceProcess.ServiceBase.
How to add some information about
our Service?
class Program :
ServiceBase
{
static void Main(string[] args)
{
}
public Program()
{
this.ServiceName = "ERP"; //Here, User puts name of Service.
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
//TODO: place your start code here
}
protected override void OnStop()
{
base.OnStop();
//TODO: clean up any variables and stop any threads
}
}
{
static void Main(string[] args)
{
}
public Program()
{
this.ServiceName = "ERP"; //Here, User puts name of Service.
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
//TODO: place your start code here
}
protected override void OnStop()
{
base.OnStop();
//TODO: clean up any variables and stop any threads
}
}
Now, we implement Service for your Application. We create
an Installer for your Application.
How to create installer for your Application?
Create
another Class for installer that is called “MyApplicationInstaller”.
Assign a
Class to Public Modifier like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyApplicationInstaller
{
public class MyApplicationInstaller
{
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyApplicationInstaller
{
public class MyApplicationInstaller
{
}
}
Add Two
Class library for installer Class.
· System.Configuration.Install
· System.ComponentModel
System.Configuration.Install:
Provides classes that allow you
to write custom installers for your own components.
System.ComponentModel:
Provides
classes that are used to implement the run-time and design-time behavior of
components and controls.
Now, we
need to configure how we want to our Service installed.
[RunInstaller(true)]
public class MyApplicationInstaller: Installer
{
public MyApplicationInstaller ()
{
var proInstaller = new ServiceProcessInstaller();
var serInstaller = new ServiceInstaller();
proInstaller.Account = ServiceAccount.LocalSystem;
proInstaller.DisplayName = "ERP";
serInstaller.StartType = ServiceStartMode.Manual; serInstaller.ServiceName = "ERP";
this.Installers.Add(proInstaller);
this.Installers.Add(serInstaller);
}
}
public class MyApplicationInstaller: Installer
{
public MyApplicationInstaller ()
{
var proInstaller = new ServiceProcessInstaller();
var serInstaller = new ServiceInstaller();
proInstaller.Account = ServiceAccount.LocalSystem;
proInstaller.DisplayName = "ERP";
serInstaller.StartType = ServiceStartMode.Manual; serInstaller.ServiceName = "ERP";
this.Installers.Add(proInstaller);
this.Installers.Add(serInstaller);
}
}
Four Members of ServiceAccount
in Windows Service:
1) LocalService
2) NetworkService
3) LocalSystem
4) User
LocalService:
An account that acts as a non-privileged
user on the local computer.
NetworkService:
An account that provides extensive
local privileges and any remote Services.
LocalSystem:
An account, used by the service control manager that has
extensive privileges on the local computer and acts as the computer on the
network.
User:
An
account defined by a specific user on the network.
Three
Members of ServiceStartMode:
1) Manual
2) Automatic
3) Disabled
Manual:
Indicates that the service is
started only manually, by a user
Automatic:
Indicates that the service is to be
started (or was started) by the operating system, at system start-up
Disabled:
Indicates that the service is
disabled, so that it cannot be started by a user or application.
Conclusion:
I hope, this Article will get some ideas
about Windows Services. Thanks for reading this article. Thanks to Google and
MSDN.
No comments:
Post a Comment