Thursday, August 30, 2012

Setup and Deployment project for Windows service

In last post we learned to create a simple windows service and used 'installutil' to install the service from console window.

In this post we will learn how to create a setup and deployment project to make the installation process easy!

1) Create a setup and deployment project. Setup project is available in 'Other Project Types' -> Setup and Deployment ->Windows Studio installer

2) Right Click on the project and Select 'Add -> Project Output'
3) Add Primary Output of the project created in previous post

4) Right click View-> Custom Actions

5) Right click on 'Install' and select 'Custom Actions'
6) Select 'File System on Target Machine' from dropdown and Select 'Application Folder' click 'Ok'
7) Select 'Primary Output from ....'
8) Do the same for install ,commit,rollback and uninstall
10) We are done! Build the project and right click and select install. This will install the service in your machine

11) To start the service go to service window (run Services.msc) and select the service and right click on the service and click start!

Tuesday, August 7, 2012

Create Windows Service using C#.NET

1) Create a console Application in .NET and name it "WinServiceTest"

2) Create a class to write data to file as given below

    public class TestWriteFile
        public static bool Shutdown = false;
        Thread fileWriteThread;
        public static void WriteCurentTime()
            while (Shutdown == false)
                string path = Path.GetDirectoryName(Assembly.GetAssembly(typeof(TestWriteFile)).CodeBase).Replace("file:\\","");
                using (FileStream ofs = File.Open(path + "\\test.txt", FileMode.Append))
                    UTF8Encoding enc = new UTF8Encoding();
                    String tmpstr = DateTime.Now.ToString() + "\n";
                    ofs.Write(enc.GetBytes(tmpstr), 0, enc.GetByteCount(tmpstr));
                Thread.Sleep(10000);//sleep for 10 seconds
        public void StartProcess()
            fileWriteThread = new Thread(new ThreadStart(WriteCurentTime));
        public void StopProcess()
            Shutdown = true;

3) Add reference to System.ServiceProcess

4) Add service class WinServiceTestService.cs which inherits from ServiceBase.cs

public class WinServiceTestService: ServiceBase
        TestWriteFile oprocessor;
        public static void Main(string[] args)
            ServiceBase.Run(new WinServiceTestService());
        public WinServiceTestService()
            this.ServiceName = "My Win Service Test";
        protected override void OnStart(string[] args)
            oprocessor = new TestWriteFile();
        protected override void OnStop()

Ok... now we have to create the installer to install the service.

5) Add reference to System.Configuration.Install

6) Write Code for installer as given below

using System.Configuration.Install;
using System.ComponentModel;
using System.ServiceProcess;

namespace WinServiceTest
    public class WinServiceTestInstaller : Installer
        public WinServiceTestInstaller()
            var processInstaller = new ServiceProcessInstaller();
            var serviceInstaller = new ServiceInstaller();

            // Service Account Information
            processInstaller.Account = ServiceAccount.LocalSystem;
            processInstaller.Username = null;
            processInstaller.Password = null;

            serviceInstaller.DisplayName = "My Win Service Test Process";
            serviceInstaller.StartType = ServiceStartMode.Automatic;
            serviceInstaller.ServiceName = "My Win Service Test Process";


7) Remove "static" from the "Main" function in Program.cs file

We are done! Windows service is ready. Now we have to install it. For installing we can use 'InstallUtil'.

Open Visual Studio command window and type "installutil /i WinServiceTest.ext" to install the service

To Start the service, open service manager right click on the task "My Win Service..." and click 'Start'

SQL statement to get domain name from URL

Given below is a SQL statement to extract domain name from URLs stored in a table
CREATE TABLE #tmpurl (url varchar(1000))

INSERT INTO #tmpurl (url)VALUES ('')
INSERT INTO #tmpurl (url)VALUES('')
INSERT INTO #tmpurl (url)VALUES('')
INSERT INTO #tmpurl (url)VALUES('')
INSERT INTO #tmpurl (url)VALUES('')
INSERT INTO #tmpurl (url)VALUES('')

case when charindex('/',substring(url,charindex('://',url)+3,len(url)))>0  
then charindex('/',substring(url,charindex('://',url)+3,len(url)))
when charindex('?',substring(url,charindex('://',url)+3,len(url)))>0  
then charindex('?',substring(url,charindex('://',url)+3,len(url)))
FROM #tmpurl