To create and deploy the WCF Service as the Windows Service in step by step. The following article describe about.
- Steps to Create Windows Service using WCF
- Steps to Deploy Windows Service using installutil.exe
Steps to Create Windows Service using WCF
- Create new project using VS2012, Select File > New Project from the Menu.
- Select Windows Service template from New Project dialog.
- Right click on designer and select Property and set ServiceName and Name
- Drag an EventLog component to the designer, from the Components section of the Toolbox.
- Press F5 and use the below code.
Sample Code
public MyFirstWindowsService() { InitializeComponent(); if (!System.Diagnostics.EventLog.SourceExists("WSMessage")) { System.Diagnostics.EventLog.CreateEventSource( "WSMessage", "NewWSLog"); } eventLog1.Source = "WSMessage"; eventLog1.Log = "NewWSLog"; } protected override void OnStart(string[] args) { string str = "Service Start at :" + DateTime.Now.ToString(); eventLog1.WriteEntry(str); } protected override void OnStop() { string str = "Service Stopped at :" + DateTime.Now.ToString(); eventLog1.WriteEntry(str); }
Now Windows service is ready and it is ready to write message to Event handler while start and stop event is triggered.
Steps to Deploy Windows Service using installutil.exe
- Right-click Service1.cs and select View Designer from Solution Explorer
- Right-click, and then click Add Installer in View Designer Screen. The two installers is added to your project inside ProjectInstaller.cs screen.
- Right click on serviceInstaller1 from ProjectInstaller.cs and make sure the Service Name property is set to MyFirstWindowsService and Start Type property is set to Automatic.
- Right click on serviceProcessInstaller1 from ProjectInstaller.cs and Set the Account property to LocalSystem. This will cause the service to be installed and to run on a local service account.
- Build the project
Steps to install
- Open the Developer Visual Command Prompt in Administrator mode
installutil.exe C:\MyNewService\bin\MyNewService.exe
- Open the Control panel and check the ‘MyNewService’ is started else start service.
Tagged: Windows Service
Leave a Reply