0

I have developed a solution which includes a Windows Service, using Visual Studio 2012. VS 2012 has removed the ability to create an installer, so I’m using a 3rd party tool for installer generation (WiX) which integrates nicely into Visual Studio.

My problem is that while WiX is able to install a service with an Automatic start/stop type, it’s unable to install a service which start up on bootup.

I have read a number of articles about starting and stopping services for non-admin users, but there seems to be some confusion about whether using a batch script with ‘net start myservice’ will actually work for a non-admin user. I’m reluctant to try something like elevating the permissions of the named user for the specified service, as it needs to work for any user who install the application suite. similarly I'm reluctant to mess with group policy because:

a) The project needs to be installed by a simple installer without the user having to fiddle with things like that b) Any user on the target machine needs to be able to start the service

So my questions are:

  1. Is there a nice, simple, guaranteed-to-work-for-a-non-admin-user way of starting a service?
  2. Is there another way to create an installer for a Windows service which is able to start the service at bootup?
  3. Is there a batch script or some other post build action that I can run as part of my installer that will make the service start on boot?

Any other suggestions on how to tackle this problem will be gratefully accepted.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
RikSaunderson
  • 3,505
  • 6
  • 32
  • 50
  • I think you are confused here. If the service is configured for Automatic start, that means the service starts immediately after Windows boots. The "start on boot" option is only for kernel services, better known as device drivers. However, if you actually want to start the service manually from your application, this question has already been asked and answered (see below). – Harry Johnston Jun 18 '13 at 04:58
  • possible duplicate of [Start Windows Service From Application without Admin right(c++)](http://stackoverflow.com/questions/8379697/start-windows-service-from-application-without-admin-rightc) – Harry Johnston Jun 18 '13 at 04:58

1 Answers1

0

It turns out that user error was at fault: my service is dependent on another service (MSMQ aka Microsoft Message Queuing), and it was trying and failing to start before MSMQ. The problems here were twofold:

  1. The attempt to access message queuing caused an unhandled exception, which was logged in the application log but not in my service-specific log (because it was unhandled)
  2. I had not included a reference to MSMQ when I installed the service.

I solved the problems by

  1. Ensuring that references to message queuing were surrounded by a try catch which logged errors to my application log
  2. Instead of starting up my service as I would any other program, I moved most of my initialisation code into another function, which returned a bool if successful. If unsuccessful I would wait x seconds and then retry up to a total of y retries, where x and y are configurable
  3. Adding a service reference in my wix installer file.

The WiX reference looks like this:

<ServiceInstall Id="ServiceNameId" Type="ownProcess" 
                Name="ServiceName" DisplayName="Service Display Name" 
                Description="Service Description" Start="auto" 
                Account="LOCALSYSTEM" ErrorControl="normal">
    <ServiceDependency Id="MSMQ" />
</ServiceInstall>
RikSaunderson
  • 3,505
  • 6
  • 32
  • 50