MSI installer for .Net 5 background service - c#

I have a .Net 5 console application that I am trying to run as a Windows service. I have followed all of the articles online on how to do this and have it completed successfully (ie: using BackgroundService). The question is, I want to have an MSI installer to deploy this and all of the articles I have read about creating a service in .Net 5 instruct me to install the service manually using a command prompt or power shell and issuing the "sc" command.
Back before .Net 5, I could use the built in Visual Studio Installer extension and simply create an MSI that would deploy and install my service. I can't seem to make that work for .Net 5.
Is it still possible to use the built in Installer extension to deploy and install my .Net 5 BackgroundService?
Thanks!

Installing Services: There are many ways to install services, you should use an MSI package and the built-in mechanisms there to install and start the service and stop and update it during upgrades.
Various ways to install services (with links to samples on how to install services using MSI).
Learning WiX: Please see the WiX section here - the links should get you started. Direct link to the main WiX link list.
Links:
Windows Services Frequently Asked Questions (FAQ)
Installation package created with WIX Toolset does not remove program folder + files on uninstallation
How to install a system service without permission errors

Related

Automate deployment of Service Fabric project without TFS or VSTS

I know you can publish a Service Fabric application written in C# using Visual Studio, and I have read this article on using TFS or VSTS to set up continuous integration DevOps builds of a Service Fabric application.
How can I just do this all manually using PowerShell? I know I can do the following using PowerShell from this article on deployment:
Use Visual Studio to package the project.
Transfer the package to a remote server.
Use the PowerShell script examples in the article to deploy the package while I am in the context of the remote server.
Instead, here are two bits I can't seem to figure out which would assist me in doing this from PowerShell:
Using PowerShell, how can I package my Service Fabric project the same way you can when you are in the context of Visual Studio?
Using PowerShell, how can I remotely deploy my Service Fabric project the same way you can when you are in the context of Visual Studio?
To generate the package through the command line, you can call the "Package" target on the sfproj file.
See my answer on create a deployment package for Service Fabric that includes all artifacts necessary to run the designed workflows at runtime
Then follow the instructions from https://azure.microsoft.com/en-us/documentation/articles/service-fabric-deploy-remove-applications/ as blackSphere suggested.
If you haven't seen this link, take a look at Deploy and Remove Packages using Powershell article.
Suppose you have a folder named MyApplicationType that contains the necessary application manifest, service manifests, and code/config/data packages. The Copy-ServiceFabricApplicationPackage command uploads the package to the cluster Image Store
That takes a directory and uploads it. Then you have to tell it to take that image and use it in the application.
The Register-ServiceFabricApplicationType command returns only after the system has successfully copied the application package. How long this takes depends on the contents of the application package. If needed, the -TimeoutSec parameter can be used to supply a longer timeout.
After you register it you can create the application:
You can instantiate an application by using any application type version that has been registered successfully through the New-ServiceFabricApplication command

create MSI for windows service in visual studio 2012

I have created a windows service in visual studio 2012. I can install the service using installutil, but I need to distribute the service to a client.
How can I build a MSI? There are wwalk throughs in vs2008, but they don't seem to apply to 2012, as 2012 has install shield limited edition and I cannot find Custom Actions anywhere..
Do you have to distribute an MSI?
If all you want is to install the service then the application itself should do it. Use a ServiceInstaller. Add a main argumnet so when you run app.exe /install it isntalls the service (and perhaps /uninstall removes it).
The following topic explains the details: How to: Add Installers to Your Service Application. The ServiceInstaller has code example.
You should use an MSI if you have more complex requirements, if you want your app to appear in the Control Panel/Add-Remove programs, if you want to add 'Repair' installs, if your installation needs to provide options (CLI or UX).

C# How to create a Setup project for a Windows Service to install it automagically?

I have seen multiple Windows Services getting installed in my computer with just an Setup.exe or .msi... is it something special that it needs to be done with the setup project to let such Windows Services installed in a easy way without Installutil.exe?
I have tried and it just doesn't install the Service. I know how to use Installutil but that I would think it's just for testing. I need to be able to install my windows service with an Setup.exe because I want to distribute my Service with its configuration Tray app.
What would be the correct way to install the service and then my app? They both use a same DLL created in the same solution.
Problem while Building a Setup Project for a windows Service?
I looked at the link above but its about the Setup Project which I already know how to do, the problem is how to make my Setup project working... my DLL is getting up in place, the Tray app is working as well, the problem is the Service not getting installed it is just giving me the .exez
but I need it to be installed correctly like be up and running at reboot and in the Service Control Manager.
Option 1
1. Download Wix
2. Download and install Wix Windows Service Setup Project Template
3. Create a new wix setup project and see what you get
This is the easiest way to create a *.msi without any option windows
Option 2
Follow this blog post. Same as option 1 but without the project template. If you go the WiX route sooner or later you will see that the project template provides basic stuff. If you want more advanced stuff you need to learn WiX a little bit and use its candle.exe, light.exe, pyro.exe etc.
Option 3
I wrote a short post how I usually do this.
Good luck
Here is a simple template for creating a *.msi installer for a Windows Service with WiX:
http://www.schiffhauer.com/wix-template-for-installing-a-windows-service/
I was able to specify my DLL dependencies with File tags.

What is the best way of automating Windows Service deployment?

I have created a windows service using C# in Visual Studio 2010. I did a lot of research about automating the installation process. Got lots of advice but none did work for me. The windows service i have created has lots of dependencies and i would like client to have no UI interaction during the installation. I have created a setup project for this which includes all my dependencies within the installer.
Process involved:
Create a build for windows service
Push the setup file (.msi) to remote location
Call the .msi and install the service silently without user interaction.
What i did so far:
Created a powershell script to push files to remote location
Execute powershell script and install the service
Please keep in mind that powershell script
sc create "servicename" binpath="somepath"
is used for installing service from a project directory not for installing .msi file created using Setup Project these are two vast different things. If you don't understand this don't answer please.
Possible solutions:
Use (http://www.msbuildextensionpack.com/help/4.0.5.0/html/258a18b7-2cf7-330b-e6fe-8bc45db381b9.htm) msbuild extension pack.
Use buildarguments to call msbuild extension pack to install windows service.
Use (http://msdn.microsoft.com/en-us/library/x8zx72cd.aspx) Exec Task
Use WIX(Windows Installer Xml)
Now my question is. How can i push windows service installer to remote location? And how can create custom build arguments and use either msbuild extension pack or Exec Task and install the service?
I know this sounds stupid and irritating question for someone who hasn't installed the service using (.MSI) created by the Setup Project. But its my requirements which i am trying to solve since weeks in the enterprise environment.
I would do the bulk of the work from the powershell script.
Use the msbuild Exec task like you mentioned to add it to your build process. Here's a pretty good article on using the exec task to run your powershell script.
Since you are using VS 2010, a setup and deployment project is pretty easy. Add one to your solution, and add the output of your service project to it. Here's an article on adding the setup project for a windows service.
Use your powershell scripts to copy the installer .msi to the remote server. You can simply use copy-item [source] [destination] if you have access to the file share.
Stop the service on the remote machine. You can use (get-service -ComputerName [destination] -Name [service-name]).Stop() (from this question)
Install the service silently using psexec psexec \\remotecomputer "[msi-destination-path]" /qn Here's the rest of the command line options for the .msi.
Start the service using (get-service -ComputerName [destination] -Name [service-name]).Start()
I'd also add a bunch of parameters to the powershell script for destination server, service name, etc. It will make maintaining this part of your build process much easier. Your build agent will most likely have to be an administrator on the destination machine as well.
Lastly, make sure to put your powershell build script in source control!
Edit (June 2014)
VS 2013 has installer projects again! (Sorry, VS 2012)
Also, if found this awesome answer about how to install a Windows Service without using a setup project.

install windows service in custom actions

I have created a c# 4.0 windows service. I have created an installer project (.msi) for it which installs the service to a particular folder.
To automate the process fully, I would like to install the service as part of the custom actions I have for my installer.
How can I code my custom actions to install or when uninstalling the msi, uninstall the windows service?
You can use the ServiceInstaller class. A quick solution would be to find installutil tool and execute it against your Service.exe, but you have to capture the output to see whether the installation succeeded or not and you don't have much control over Install, Commit, Rollback, and Uninstall phases.
Simple answer: don't. The proper way is to install it using the MSI database itself, i.e. ServiceInstall and ServiceControl tables. Every single "convenient" IDE for MSI creation and also WiX come with primitives to make use of this builtin facility.
The very reason that this is best practice, just like including the COM registration in your MSI instead of calling DllRegisterServer of the COM (DLL) to register is that your application may be defunct at the time the user attempts to remove it.
The database actions can still be executed even by a newer Windows Installer, say after an upgrade of Windows itself, while your code may refuse to run or may not run for other reasons.

Categories