Search This Blog

Wednesday, February 27, 2013

Windows Service with Setup installer.

Add a project WindowsServiceCSharp


A file service1.cs will be added automatically.
Service1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;

namespace WindowsServiceCSharp
{
   public partial class Service1 : ServiceBase
   {
      public Service1()
      {         InitializeComponent();
        //tm_Elapsed(null, null);
      }

      protected override void OnStart(string[] args)
      {
        Timer tm = new Timer();
        tm.Interval = 60000D;
        tm.Elapsed +=new ElapsedEventHandler(tm_Elapsed);
      }
      void tm_Elapsed(object sender, ElapsedEventArgs e)
      {// do code here.
        //this.Dispose();
      }
      protected override void OnStop()
      {

      }

   }
}
Right click on Service1.design file and an add Installer as below.


Now a new file will be added ProjectInstaller.cs



Do the coding as per you. Now add a setup project into the solution.



Choose setup project and click on Ok.
Now right chilk on setup project(WSSetup) and add a project output.



If you want to give some user input at service installation then right click on setup project WSSetup and add a user interface as below.



Now right click on statrt and add dialog and choose a interface. (In my case TextBoxes(A)) < br />
Now add custom option to call service installer at installation.

Set CustomActionData ...


Now override the Install method of ServiceBase class in projectinstaller class as below. Before calling the base class's install method you can change the service related initialization like setting service name at installation time.(in my case SetServiceName() is called to set servivice name)
Note: User Interface input can onlu=y be access here Install method.
ProjectInstaller.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Configuration;

namespace WindowsServiceCSharp
{
   [RunInstaller(true)]
   public partial class ProjectInstaller : System.Configuration.Install.Installer
   {
      public ProjectInstaller()
      {
         InitializeComponent();
      }

      public override void Install(IDictionary stateSaver)
      {
         SetServiceName();
         base.Install(stateSaver);
         try
         {
            string targetDir = Context.Parameters["targetdir"];
            string prm1 = Context.Parameters["Param1"];
            string prm2 = Context.Parameters["Param2"];
            string prm3 = Context.Parameters["Param3"];
            string prm4 = Context.Parameters["Param4"];

            string exePath = String.Format("{0}WindowsServiceCSharp.exe", targetDir);



            Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);

            config.AppSettings.Settings["Param1"].Value = prm1;
            config.AppSettings.Settings["Param2"].Value = prm2;
            config.AppSettings.Settings["Param3"].Value = prm3;
            config.AppSettings.Settings["Param4"].Value = prm4;
            System.IO.File.WriteAllText(@"D:\test.txt", prm4);
            config.Save();
         }
         catch (Exception ex)
         {
            throw new Exception(ex.Message);
         }
      }
      void SetServiceName()
      {
        this.serviceInstaller1.ServiceName = Context.Parameters["Param1"];

     }
   }
}
Now build the service and setup project.
Now setup file ready to install windows service.

Wednesday, February 20, 2013

How to concatenate columns separated by comma using sql xml.

Concatenate the Revision against soft
SoftRevision
11
12
13
22
24
35


Select Soft , LEFT(Revision , LEN(Revision)-1) as Revision
FROM
(
Select Soft,
( Select Convert(VARCHAR,Revision)+',' from tblName x where x.soft= y.soft
for xml path('')
) as revision
from tblName y
group by soft
)ask

output
SoftRevision
11,2,3
22,4
35