|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionAs a matter of fact Microsoft Windows services, formerly known as NT services enable you to create long-running executable applications that run in its own Windows session, which then has the ability to start automatically when the computer boots and also can be manually paused, stopped or even restarted. This makes services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer. You can also run services in the security context of a specific user account that is different from the logged-on user or the default computer account. Windows services don’t have any interface to the user, so it can not be debugged like any regular application, but it’s debugged as a process. .NET has a very nice tool that enables processes debugging while it’s in the run status, by easily pressing Ctrl + Alt + P shortcut. BackgroundI’ve searched well so many sites about a code that I can with the help of it, build a simple Windows service, but I found a lot of code on how to manage the current Windows services of the system and that’s through the After searching the MSDN, I’ve found some nice code that helped me to create this simple Windows service. Hope it can help as a basic architecture for and usage of such a Windows service. Using the codeAt first you should simply open VS.NET and then at the File menu click on New, Project. From the New Project Dialog Box, choose the Windows service template project and name it MyNewService like shown below:
The project template automatically adds a component class that is called Click the designer. Then, in the Properties window, set the Set the In the code editor, edit the static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
//Change the following line to match.
ServicesToRun = new
System.ServiceProcess.ServiceBase[] { new MyNewService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
In the next section, you will add a custom event log to your Windows service. Event logs are not associated in any way with Windows services. Here the To add custom event log functionality to your service:
To access the constructor in Visual C#, expand the Component Designer generated code region. public MyNewService()
{
InitializeComponent()
if(!System.Diagnostics.EventLog.SourceExists("DoDyLogSourse"))
System.Diagnostics.EventLog.CreateEventSource("DoDyLogSourse",
"DoDyLog");
eventLog1.Source = "DoDyLogSourse";
// the event log source by which
//the application is registered on the computer
eventLog1.Log = "DoDyLog";
}
To define what happens when the service starts, in the code editor, locate the protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("my service started");
}
The To define what happens when the service is stopped, in the code editor, locate the protected override void OnStop()
{
eventLog1.WriteEntry("my service stoped");
}
You can also override the protected override void OnContinue()
{
eventLog1.WriteEntry("my service is continuing in working");
}
Some custom actions need to occur when installing a Windows service, which can be done by the
TipTo avoid being asked about the system username and password you must change the [RunInstaller(true)]
public class ProjectInstaller : System.Configuration.Install.Installer
private System.ServiceProcess.ServiceProcessInstaller
serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
/// <summary>
/// Required designer variable.
/// </summary> private System.ComponentModel.Container components = null;
public ProjectInstaller()
// This call is required by the Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
private void InitializeComponent()
{
this.serviceProcessInstaller1 =
new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 =
new System.ServiceProcess.ServiceInstaller();
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "MyNewService";
this.serviceInstaller1.StartType =
System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange
(new System.Configuration.Install.Installer[]
{
this.serviceInstaller1,
this.serviceInstaller1});
}
}
To build your service project
Now that the project is built, it can be deployed. A setup project will install the compiled project files and run the installers needed to run the Windows service. To create a complete setup project, you will need to add the project output, MyNewService.exe, to the setup project and then add a custom action to have MyNewService.exe installed. To create a setup project for your service
A setup project is added to the solution. Next you will add the output from the Windows service project, MyNewService.exe, to the setup.
To add MyNewService.exe to the setup project
To add a custom action to the setup project
To install the Windows ServiceBrowse to the directory where the setup project was saved, and run the .msi file to install MyNewService.exe.
To start and stop your service
Right-click the service, and then click Stop.
To verify the event log output of your service
To uninstall your service
|
||||||||||||||||||||||