by Nitin on Thursday, 16 September 2010
Create Windows Service


To add custom event log functionality to your service

  1. In Solution Explorer, right-click Service1.vb or Service1.cs and select View Designer.
  2. From the Components tab of the Toolbox, drag an EventLog component to the designer.
  3. In Solution Explorer, right-click Service1.vb or Service1.cs and select View Code.
 Add or edit the constructor to define a custom event log.
public MyNewService() 	{
		System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog(); 		InitializeComponent(); 		if (!System.Diagnostics.EventLog.SourceExists("MySource"))  		{          				System.Diagnostics.EventLog.CreateEventSource( 					"MySource","MyNewLog"); 		} 		eventLog1.Source = "MySource"; 		eventLog1.Log = "MyNewLog"
} 
 

To define what occurs 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 starts running:

     

     

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

    To define what occurs when the service is stopped

    • In the Code Editor, select the OnStop procedure from the Method Name drop-down list, which was automatically overridden when you created the project. Write code to determine what occurs when the service is stopped:

       

       

      protected override void OnStop() 	{ 		eventLog1.WriteEntry("In onStop."); 	}
      You can also override the OnPauseOnContinue, and OnShutdown methods to define additional processing for your component.

      Some custom actions have to occur when a Windows service is installed, 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

      1. In Solution Explorer, right-click Service1.vb or Service1.cs and select View Designer.

      2. Click the background of the designer to select the service itself, instead of any of its contents.

      3. With the designer in focus, right-click, and then click Add Installer.

        By default, a component class that contains 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.

      4. In Design view for ProjectInstaller, click ServiceInstaller1 or serviceInstaller1.

      5. In the Properties window, make sure the ServiceName property is set to MyNewService.

      6. Set the StartType property to Automatic.

      7. In the designer, click ServiceProcessInstaller1 (for a Visual Basic project), or serviceProcessInstaller1 (for a Visual C# project). Set the Account property to LocalSystem. This will cause the service to be installed and to run on a local service account.

      To build your service project

      1. In Solution Explorer, right-click your project and then click Properties. The project's Property Designer appears.

      2. On the Application page, from the Startup object list, click MyNewService.

      3. Press CTRL+SHIFT+B to build the project.

      Now that the project is built, it can be deployed. A setup project will install the compiled project files and run the installers that are required to run the Windows service. To create a complete setup project you will have to add the project output, MyNewService.exe, to the setup project and then add a custom action to have MyNewService.exe installed. For more information about setup projects, see Setup and Deployment Projects. For more information about custom actions, see Walkthrough: Creating a Custom Action.

      To create a setup project for your service

      1. In Solution Explorer, right-click your solution, point to Add, and then click New Project.

      2. Under Installed Templates, expand Other Project Types and then expand Setup and Deployment.

      3. Select Visual Studio Installer.

      4. In the Templates pane, select Setup Project. Name the project MyServiceSetup. Click OK.

        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

      1. In Solution Explorer, right-click MyServiceSetup, point to Add, and then click Project Output.

        The Add Project Output Group dialog box appears.

      2. MyNewService is selected in the Project box.

      3. From the list, select Primary Output, and click OK.

        A project item for the primary output of MyNewService is added to the setup project.

      Now add a custom action to install the MyNewService.exe file.

      To add a custom action to the setup project

      1. In Solution Explorer, right-click the setup project, point to View, and then click Custom Actions.

        The Custom Actions editor appears.

      2. In the Custom Actions editor, right-click the Custom Actions node and click Add Custom Action.

        The Select Item in Project dialog box appears.

      3. Double-click the Application Folder in the list to open it, select Primary Output from MyNewService (Active), and click OK.

        The primary output is added to all four nodes of the custom actions — InstallCommitRollback, and Uninstall.

      4. In Solution Explorer, right-click the MyServiceSetup project and click Build.

      To install the Windows Service

      1. To install MyNewService.exe, right-click the setup project in Solution Explorer and select Install.

      2. Follow the steps in the Setup Wizard. Build and save your solution.

      To start and stop your service

      1. To open the Services Control Manager in Windows 7, Windows Vista, and Windows Server, right-click Computer on the Start menu, and then click Manage. In theComputer Management console, expand the Services and Applications node in the left pane. Click Services.

        You should now see MyNewService listed in the Services section of the window.

      2. Select your service in the list, right-click it, and then click Start.

      3. Right-click the service, and then click Stop.

      To verify the event log output of your service

      1. Open Server Explorer and access the Event Logs node.

        NoteNote

        The Windows Service template and associated functionality is not available in the Standard Edition of Visual Studio.

      2. Locate the listing for MyNewLog and expand it. You should see entries for the actions your service has performed.

      To uninstall your service

      • On the Start menu, open Control Panel and click Add or Remove Programs, and then locate your service and click Uninstall.




       

     

 
 
 
 
 
 
 



Blogger Image
My Blog Title

Product Engineering, software engineering company, Product Development, Product Migration, Product Re-engineering, Product Maintenance, Product Testing Commercial Application Development, Business Software development, commercial software for startups, Application Support and Maintenance, software testing Product Maintenance, Outsource product maintenance, product support and maintenance Product Migration, Product Re-engineering, product re-engineering services Product Research, Product Engineering, UI Prototyping Services Software Testing Services, Quality Assurance services, professional software testers, Load Testing, Functional Testing, Cross Platform, Browser Testing, Test Automation, Testing Tools, software quality analysis Functional Testing Services, software quality analysis, Software Testing Services, Application Testing Services, Functional Testing Types Automated Testing, Automated Testing Services, automation testing, test script development, Automation Test Tools, outsource automation testing Load Testing, Performance Testing Services, Load Testing Tools Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Benefits of IT Outsourcing, Offshore Software Development companies, offshore software development firms Outsource planning, IT outsourcing, IT development services, offshore IT companies, offshore software development Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering