Search code examples
c#visual-studio-2013windows-serviceswindows-installer

C# How to install Windows Service as part of application installation


I have created an installer that takes the primary output from a selection of sub-projects, one of the sub-projects that make up my application is a Windows Service. This service needs to be installed as part of the main application.

So far I have tried adding an installer to the Service project, publishing this separately and installing the resultant Setup.exe as a custom action for the project installer but have not had any success with this method. I would install the thing from the command line, however I'd need to then package in the Visual Studio CMD Tool to get access to the installutil command.

My question is, can I install a Windows Service from a "Visual Studio Installer - Setup Project" as part of a larger project and if so how? As the process has eluded me thus-far.

I suppose I could try having the custom action trigger a script that executes the ServiceInstaller as part of the process, so I'll be trying that.

Thanks in advance


Solution

  • Since VS2013 does not contain the old (VS2010) Visual Studio installer project, I have these suggestions:

    Use WiX installer: https://wix.codeplex.com

    There is even a service installer template (not tested):

    https://visualstudiogallery.msdn.microsoft.com/7f35c8ce-1763-4340-b720-ab2d359009c5


    Use VS 2013 install extension

    There were many complaints that MS abandoned the old setup project, so they brought it back as an extension:

    http://blogs.msdn.com/b/visualstudio/archive/2014/04/17/visual-studio-installer-projects-extension.aspx

    I have not yet tested this, but as I understand it, it will bring back the old capabilities of the internal VS setup project type.


    I have written some Windows services using VS2010 setup project and even wrote a step-by-step instruction how to do this. Some steps are not self-explanatory, so I use this myself as a guide for service projects.

    I did some first tests with WiX, which seems a good solution, however I had not yet used that for service installation.

    Another link regarding this topic:

    https://superuser.com/questions/727643/installing-a-windows-service-without-visual-studios-installutil-exe

    I plan to check my step-by-step instructions regarding service installation with VS2013 soon and probably compare this to WiX, if you are interested, please tell me, I can make this publicly available.


    Here are my step-by-step instructions for creating a Windows service. (you can find this also on http://www.rsprog.de/samplewindowsservice)

    • File => New => Project...

    • Visual C# => Windows Desktop => Windows Service

    • Name: SampleService

    • "Create directory for solution" checked

    • In Solution Explorer, select Service1.cs, right-click => Rename

    • Rename file to SampleService.cs

    • Select Yes for "... Would you also like to perform a rename ..."

    SampleService.cs should be shown in Design View now

    • Click in the background of the designer view

    • Right-click => Add Installer

    (serviceProcessInstaller1 and serviceInstaller1 have been added)

    • Open SampleService.cs in design view

    • In Properties, set ServiceName to SampleService

    • Open ProjectInstaller.cs in Design View

    • Click serviceInstaller1

    • In the Properties window, set the ServiceName property to SampleService.

    • Set the DisplayName property to Sample Service or something else (This will be shown later in services console as service name)

    • Here you can also optionally set StartType from Manual to Automatic

    • Click serviceProcessInstaller1

    • Edit Account: Here you can change if the service will run under a technical account (User) or e.g. as LocalSystem

    If you set it as User, a popup during setup will allow user / password to be set

    • At Solution right-click => Add => New Project...

    • Other Project Types => Visual Studio Installer => Setup Project

    • Name: SampleServiceSetup

    • Select SampleServiceSetup project, right-click => Add => Project Output...

    • As Project "SampleService => Primary output" should be already selected, as Configuration "(Active)" should be already selected.

    • Press OK

    • Select SampleServiceSetup project, in properties view:

    • Change InstallAllUsers to True

    • Change ProductName to SampleService (remove Setup at the end)

    • Change Title to SampleService (remove Setup at the end)

    By default, the target platform of the setup project is x86. You can change it here to x64 (TargetPlatform)

    • Right-click the setup project in Solution Explorer => View => Custom Actions

    • In the Custom Actions view, right-click the Custom Actions node => Add Custom Action...

    • Double-click the Application Folder in the list to open it, select Primary Output from SampleService (Active), and click OK. ("Primary output from SampleService (Active)" was added to the four nodes)

    • Select SampleServiceSetup project, right-click => View => User Interface

    • Start => Installation Folder => Properties => InstallAllUsersVisible: change to False

    • Build solution