How to create windows sevice in Dotnet.


How to create windows sevice in Dotnet. Windows service 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.

How to create windows sevice in Dotnet.


1) 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. The project template automatically adds a component class that is called Service1 by default and inherits from System.ServiceProcess.ServiceBase.

2) Click the designer. Then, in the Properties window, set the ServiceName property for Service1 to MyNewService.

3)Set the Name property to MyNewService. Set the AutoLog property to true.

4)In the code editor, edit the Main method to create an instance of MyNewService. When you renamed the service in step 3, the class name was not modified in the Main method. To access the Main method in VC#, expand the Component Designer generated code region.

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);
}

5) 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 EventLog component is used as an example of the type of components you could add to a Windows service.

To add custom event log functionality to your service:

In the Solution Explorer, right-click Service1.vb or Service1.cs and select View Designer.
From the Components tab of the Toolbox, drag an EventLog component to the designer.
In the Solution Explorer, right-click Service1.vb or Service1.cs and select View Code.
Edit the constructor to define a custom event log.

Constructor Code here :

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";
}


6) To define what happens when the service starts, in the code editor, locate the OnStart method that was automatically overridden when you created the project, and write code to determine what occurs when the service begins running:

protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("my service started");
}

7) The OnStart method must return to the operating system once the service's operation has begun. It must not loop forever or block. To set up a simple polling mechanism, you can use the System.Timers.Timer component. In the OnStart method, you would set parameters on the component, and then you would set the Timer.Enabled property to true. The timer would then raise events in your code periodically, at which time your service could do its monitoring.

To define what happens when the service is stopped, in the code editor, locate the OnStop procedure that was automatically overridden when you created the project, and write code to determine what occurs when the service is stopped:


protected override void OnStop()
{
eventLog1.WriteEntry("my service stoped");
}

8) Some custom actions need to occur when installing a Windows service, which can be done by the Installer class. Visual Studio can create these installers specifically for a Windows service and add them to your project. To create the installers for your service.

Return to design view for Service1.
Click the background of the designer to select the service itself, rather than any of its contents.
In the Properties window, click the Add Installer link in the gray area beneath the list of properties. By default, a component class containing two installers is added to your project. The component is named ProjectInstaller, and the installers it contains are the installer for your service and the installer for the service's associated process.
Access design view for ProjectInstaller, and click ServiceInstaller1.
In the Properties window, set the ServiceName property to MyNewService.
Set the StartType property to Automatic.


Comments

Author: ketan Italiya29 Aug 2013 Member Level: Gold   Points : 6

Create a Setup project for a Windows Service
Create a Setup project for a Windows Service
This section describes how to create a Windows Service project and how to use a compiled Setup project to install the Windows Service.
Create a Windows Service project
1. Click Start, point to Programs, point to Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and then click Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
2. On the File menu, point to New, and then click Project.
3. In the New Project dialog box, follow these steps:
a. Under Project Types, click Visual Basic Projects or click Windows under Visual Basic.
b. Under Templates, click Windows Service.
c. In the Name box, type LogWriterService.
d. In the Location box, type C:\, and then click OK.
4. In Solution Explorer, right-click Service1.vb, and then click View Code.
5. In the OnStart event handler, replace the comments with the following code.

EventLog.WriteEntry("My simple service started.")

6. In Solution Explorer, double-click Service1.vb.
7. In the Properties dialog box, click Add Installer.
8. In the Properties dialog box for ServiceInstaller1, change the ServiceName property to LogWriterService.
9. In Design view, click ServiceProcessInstaller1 in the Code Editor.
10. In the Properties dialog box, change the Account property to LocalSystem. The LocalService value and the NetworkService value are only available in Microsoft Windows XP and later operating systems.
Use a compiled Setup project to install the Windows Service
After you complete the steps in the "Create a Windows Service project" section to configure the Windows Service project, you can add a deployment project that packages the service application so that the service application can be installed. To do this, follow these steps:
1. Add a new project to your LogWriterService project.
a. In Solution Explorer, right-click Solution 'LogWriterService', point to Add, and then click New Project.
b. Under Project Types, click Setup and Depl

Author: Phagu Mahato16 Nov 2013 Member Level: Gold   Points : 8

you can use a Visual Studio project template called Windows Service. This template automatically does much of the work for you by referencing the appropriate classes and namespaces, setting up the inheritance from the base class for services, and overriding several of the methods you're likely to want to override.

To create a Windows Service application

Create a Windows Service project.

In the Properties window, set the ServiceName property for your service.

Access the Code Editor and fill in the processing you want for the OnStart and OnStop procedures.

Override any other methods for which you want to define functionality.

Add the necessary installers for your service application.

Build your project by selecting Build Solution from the Build menu.

Example of function for windows Services


Timer timer = new Timer();
public ScheduledService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
TraceService("start web service");

timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);


timer.Interval = 60000;

timer.Enabled = true;
}
protected override void OnStop()
{
timer.Enabled = false;
TraceService("stopping service");
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
TraceService("Another entry at "+DateTime.Now);
}
private void TraceService(string content)
{

FileStream fstream = new FileStream(@"d:\ScheduledService.txt",FileMode.OpenOrCreate, FileAccess.Write);

StreamWriter Streawrite1 = new StreamWriter(fstream);

Streawrite1.BaseStream.Seek(0, SeekOrigin.End);

Streawrite1.WriteLine(content);

Streawrite1.Flush();
Streawrite1.Close();
}



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: