I have created windows service project (WinService.exe) using C#. Also I added installer capability with the project(ProjectInstaller.cs) as per the below guide from Microsoft:
https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer
Now, when I performed install and uninstall using installutil.exe, my windows service project adds the service into services panel and removes from it appropriately.
As I want to deploy this service into remote machine, I created windows installer project(DeployService.msi) using VisualStudio 2015 as a service deployment project. Also, I configured custom action for Install, UnInstall, Commit and Rollback targeting primary output as WinService project.
When I perform installation using this installer, the service gets added into services panel and ApplicationFolder copies all binaries required for service. But, when I perform uninstall, ApplicationFolder binaries are removed but it leaves a InstallState file i.e WinService.InstallState. IMPORTANTLY, the service is not removed from services panel.
Any help here to remove service from services panel via windows installer?
I have tried adding event handler for ServiceProcessInstaller and noticed that OnBeforeUninstall() and OnAfterUinstall() are never called by windows installer due to some reason. At same time, I noticed OnBeforeInstall() and OnAfterInstall() are called. This is main reason why windows service was not uninstalled in my case.
When I tried overriding ProjectInstaller (derviced from Installer) class methods:
protected override void OnBeforeUninstall(IDictionary savedState);
protected override void OnAfterUninstall(IDictionary savedState);
I observed windows installer calls these methods appropriately and I have written appropriate methods to remove windows service there.
You can try to execute the next command when you execute the uninstall of the application, which removes the service with the exact name (you can try it using the CMD):
sc delete “serviceName”
Related
i am new to windows services. please help me to know is Project installer Mandatory for Windows service or why do we need project installer. can we create and install windows service into system without project installer.
thanks,
If you're talking about the ProjectInstaller.cs file that's added to your service project if you're looking at the Service and choose "Add Installer...", then yes, it's required.
It creates two components - a ServiceProcessInstaller and a ServiceInstaller. Together, these components contain important information that is required to install a service - such as what account the service should run under (such as Local System, Local Service, Network Service or an actual user account), metadata about the service, and information about how the service should be started.
I think that DinosaurTom's answer was assuming you were talking about an installer project, a separate project in the solution that would create a setup/MSI file. It is exactly the two above mentioned components that tools such as InstallUtil or a Setup Project actually interact with to perform installation.
No, it is not mandatory. However it could be useful when installing on many maschines.
We can create a Windows Service without Project Installer. For example, like in this post Install a Windows Service using a Windows Command Prompt
I am creating a setup project for my application in vs12, my application has a windows form and a windows service, how can I add a service in installer using inno setup Compiler? or any other setup creator?
I used Install shield limited version before but i am unable to create app folder in program data with that.
You can do this by creating a Custom Action in your Setup Project and then you install the User Service silently by modifying registries. There are some example that you can find on the internet. One of the good examples i have found from Code Project site.
Install Windows Service using Custom Actions
I have created an installer which installs windows service. I have used Visual studio default installer in VS 2010.
The service will access and write on the database file(SQLite) present in the installation folder.
During uninstall when the service is stopped or running, the service is uninstalling without any error.
But if the installer is migrated from version 1 to 2, and the service of previous version is running then"The file is being accessed by some application. Try again" error has been shown, since the previous installation service accessing the DB file.
So I need to stop the previous version service before Installing new version.
I have a custom action script for managing install and uninstall.
In BeforeInstall method I have written code to stop the service using ServiceController. That can stop the service.
ServiceController service = new ServiceController("SERVICE_NAME");
if (!(service.Status.Equals(ServiceControllerStatus.Stopped)))
{
service.Stop();
service.WaitForStatus (System.ServiceProcess.ServiceControllerStatus.Stopped);
}
But even before BeforeInstall method the installer is copying new version files replacing my previous version files.
I always have same installation folder.
So if I write code to stop the service in BeforeInstall custom Action it will still throw error, because the DB file being accessed by previous version service was tried to delete by new version.. So I am getting "The file is being accessed by some application. Try again".
So I need some hook even before the installer copies the files to the installation folder. So that I can stop the service before the installer try to update the Db file.
Any idea would be appreciated.
Windows Installer doesn't have a concept of "before install". This is a Visual Studio Setup and Deployment project abstraction. This project type failed to expose many underlying Windows Installer features and this is why Microsoft eliminated it in VS2012.
Another concept that isn't exposed is the ServiceInstall and ServiceControl tables. This is why you are being forced to write custom actions. Custom actions that are hosted in such a way ( beyond your control ) to tattoo the MSIEXEC process with a CLR version and throw modal 1001 exceptions (even during silent installation) when there is a problem.
But there is a solution. Switch to a better tool such as free and open source Windows Installer XML (WiX) and Industrial Strength Windows Installer XML (IsWiX). With these tools you can create a windows service, create the installer for it and test the install/uninstall on a VM in 10 minutes. Watch my silent demo at:
Building and Deploying a Windows Service using IsWiX
I have made a Windows Service that reads an excel sheet and updates the values in database. The service just works fine when I install it from the visual studio command prompt.
I have also made an exe file using the other projects option in Visual Studio. When I install that the exe I made, I cannot see my service in services panel of control panel=>System and Security=>Administrative tools=>Service.
I've run into similar issues with production services I've made.
Carefully follow the solution as described here:
http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.80).aspx
Note that the key is creating the "installer" service class in the service project. This creates the output you will have the MSI "install" and will specify what account will be used to run the service, etc.
I've created a windows service in c# and I'm trying to install it for debug using the installutil as recommended here: http://msdn.microsoft.com/en-us/library/sd8zc8ha.aspx
The installutil says Install completed. However, nothing appears in the service control manager.
I've tried this on Server 2008 and XP with the same result.
Any ideas?
A colleague of mine had a more or less identical problem.
Did you add an installer to your project? For the service to be installed you need to add an installer to your Visual Studio Project.
The easiest way to add an installer in Visual Studio is to open your service in Design Mode and right click the design area and select Add Installer. This will add a file ProjectInstaller.cs with itself contains a ServiceInstaller object and a ServiceProcessInstaller object.
With the installer added you can set the Service Name, Description and other options that will be used when installing the service. If you now try to use InstallUtil your service should be installed and should show up in the Services list.
I had a similar issue (build installer, no errors, no service appears in services.msc) but a different solution, as I had configured the installers.
In my case, the Service-project's Application Properties (Alt-Enter, Application-tab) Startup object was (not set) as shown below:
Picking the default .Program and rebuilding service and installer worked (service appeared in services.msc).
Setting this property is one of the steps in the MSDN service installer walkthrough referenced in this SO answer. Make sure to follow all of the steps!
I can't speak specifically to any issues that are the fault of C# or .NET, but I have a writeup of what has to happen for a service to be installed and work in the form of an extensively documented framework (source code included) for writing services in Lua. I offer it up here as an example of another way to do it, because sometimes just seeing things from another point of view can be helpful.
Disclaimer: It is very much at an alpha quality level, and yes, I am its author.
The framework has all the code needed to interact with the Windows SCM to install and remove the service.