How to deploy and start Windows Service?

M

Mika M

I'm doing Windows Service programming using C# 2005. My solution
contains Windows Service project. Then I added Setup and Deployment
project into the same solution. Both service- and Setup and Deployment
project are working fine. After running setup, it made files into the
C:\Program Files\MyCompany\MyService, but how can I install service into
the services and start this service using only Setup and Deployment project?

Now I can do this using install.bat-file containing...

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil /i MyService.exe
NET START MyService

....and uninstall it using ininstall.bat-file containing...

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil /u MyService.exe

Or am I doing this correct way at all? What is the correct way to
install this kind of service easily?
 
C

Claire

Go into visual studio help and look for "services, adding installers"

In my vis studio 2005 help it's at url
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_radcon/html/8b698e9a-b88e-4f44-ae45-e0c5ea0ae5a8.htm
I've not worked out yet how to get the service to start after installation,
so if you work it out then please post back here lol
 
M

Mika M

My deployment project was missing Custom Actions. I found instructions
of the address http://support.microsoft.com/kb/816169

Now it's going into services ok, but I'm having the same problem as you
- how to start this service using only Setup and Deployment project without:

1. booting computer
2. or going into Start/Control Panel/Administrative Tools/Services and
start it there.
3. or using NET START MyService -command

thanks for the tip anyway!

Claire kirjoitti:
 
C

Claire

Hi again :)
Try adding something like the following into the OnInstalled/Committed event
of your ProjectInstaller class. I've only tested the following inside a
regular form so far as Im not quite sure which event I should be trapping

using System.ServiceProcess;

[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
string servicename = "my.service";
System.ServiceProcess.ServiceController[] services;
services = System.ServiceProcess.ServiceController.GetServices();
bool Found = false;
foreach (ServiceController sc in services)
if (string.Compare(servicename, sc.ServiceName, true) == 0)
Found = true;
if (Found)
{
ServiceController Controller = new ServiceController(servicename);
if (Controller.Status == ServiceControllerStatus.Stopped)
Controller.Start();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top