Windows Service Installation - c#

Scenario
I have a server, that has NO Visual Studio Installed. It literally has a normal command prompt and nothing installed yet. We don't want to install anything (except the .Net framework which we have already done). We just want to install a bunch of C# Windows Services that we have written.
So far
I have been installing and running the windows service on my local machine using a "setup and deploy" project that I built into the application, which I could then use to install the service locally.
Question
How can I install the service on the server?
I imagine it can be done from the command prompt only, but what else do I need? - If anything? and where do I put the files that I want to install BEFORE I install them?
I imagine I will have to compile the application on my local machine in Visual Studio, then copy it over to the server, and then run an install utility to install it on the server?
Any help would be greatly appreciated.

according to this thread you need to run
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe C:\MyService.exe
(replace c:\windows with your windows path)

Your server has a sc.exe (service control) command which allows you to install, uninstall, start, stop and configure services - no Visual Studio bits needed.
Run sc.exe -? at a command prompt to get a listing of all available options

I add code similar to the one in this article to my services:
http://www.codeproject.com/KB/dotnet/WinSvcSelfInstaller.aspx
Then I can install/uninstall them just by typing ServiceName -i or ServiceName -u at the command prompt. Makes it easier if it'll be installed by people who don't know .Net.

Related

Install Visual Studio ClickOnce applications on Windows 10 from Ansible when manufacturer cannot be verified

I have an Ansible playbook connected to hosts with Windows 10. I need to install a couple of applications that I develop in Visual Studio on those hosts. For this, I compile these applications with the publish option of Visual Studio. This sends me to the ClickOnce publication wizard, in which it generates a setup.exe and a .application.
I have tried several ways to run these files, with ansible.windows.win_command, ansible.windows.win_shell and ansible.windows.win_package, but none have worked for me, and I don't get any error in the output of the command. When I go to one of these hosts and run the setup.exe or the .application from PowerShell the following window appears:
Unable to verify manufacturer. Confirm that you want to install this application?
Which asks me, "Unable to verify manufacturer. Confirm that you want to install this application?" and I need to click on install, but the idea is that I should automate this process with Ansible since the hosts should not have interaction with a human.
I want to do this without the need to create credentials or register a manufacturer, maybe with some configuration in the Windows Registry. It should be some command that allows me to install without asking or some configuration when publishing my application from Visual Studio.

Is it possible to debug a C# console application using git bash?

I am using Visual Studio Community 2022, and I was wondering if you could use another CLI to run/debug a c# console application. (I can't use cmd.exe or powershell because it's blocked by the admins of the device I'm using). Git Bash would be preferred, because it's already installed. Thank you!
Edit: Looks like you can't do this. dotnet run just tries to open the blocked binary, no matter where you run it. I think the admins hate everyone though because they allow you to install lots of things (like Unity, vscode, etc.) but running anything just doesn't work.
Thanks for trying everyone!
use this to run your program in CLI but you cannot debug it if you want to debug use debugger mode of vs2022,
If it's a framework-dependent application (the default), you run it by dotnet yourapp.dll.
Run the project in the current directory:
dotnet run
check Microsoft Docs dotnet run for more options.
CLI: Use Vs2022 Terminal in View->Terminal it may help You to run it.

How to install a C# Windows Service on a remote server?

Hi I have developed a C# Windows Service in Visual Studio.
I am able to install this service on my local machine and it works fine.
Now I want to be able to install it on a remote server.
Can you tell me how to do this?
My service is just built on the Windows Service VS template, so it's all very simple.
I am not so geeky, so it would be useful with some tutorial, manual which I can understand.
I am running VS 2010 Professional.
You need to have Remote Desktop access to the server.
When you are in you can do it via the commandline using something like this:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\installutil /LogToConsole=true C:\Path\To\Service.exe
Then you can manage it (start it, set it to auto start, stop it, restart it) by going to Start | Run and typing
services.msc
then press enter.
To uninstall it use:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\installutil /u /LogToConsole=true C:\Path\To\Service.exe
But you will need to have stopped the service first.
Note: There is probably a new util in newer .net releases - my notes are from a while ago when I built a 2.0 service. Look in C:\Windows\Microsoft.NET\Framework\ for a version number that matches the .net you're developing in.
Use the SC command.
sc \\remotecomputer create newservice binpath= C:\Windows\System32\Newserv.exe
start= auto obj= DOMAIN\username password= pwd

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.

Installing a Windows Service -Setup.exe fails

There is a weird issue I am facing. I created my first windows service looking through various blogs and tutorials.
Then created setup for that adding installer etc. It works fine while installing , un-installing via Visual Studio but it fails when i deploy it .
Below is a summary of the errors, details of these errors are listed later in the log.
* Activation of C:\Users\bhuvint\Documents\Visual Studio 2010\Projects\CPNS Library\ServicePackage\CommonPushNotificationWindowsService.application resulted in exception. Following failure messages were detected:
+ You cannot start application CommonPushNotificationWindowsService from this location because it is already installed from a different location.
+ You cannot start application CommonPushNotificationWindowsService from location file:///C:/Users/bhuvint/Documents/Visual%20Studio%202010/Projects/CPNS%20Library/ServicePackage/CommonPushNotificationWindowsService.application it is already installed from location file:///C:/inetpub/wwwroot/ServicePackage/CommonPushNotificationWindowsService.application. You can start it from location file:///C:/inetpub/wwwroot/ServicePackage/CommonPushNotificationWindowsService.application or you can uninstall it and reinstall it from location file:///C:/Users/bhuvint/Documents/Visual%20Studio%202010/Projects/CPNS%20Library/ServicePackage/CommonPushNotificationWindowsService.application.
I have already un-installed the service and trying to install it from the deployed service in the same pc to test. But it fails with above error.
I feel i have deployed the windows service the wrong way. Could you guys please suggest how to deploy a windows service ?? (I have looked through net. All suggest building it and installing it from visual studios. But how to publish it is the thing which i need.)
Please help asap.
Possibly you built the setup project before uninstalling the preovious service. In that case you can't use the new setup to uninstall the service, since the setup won't be the same and can't be able to access the service installed.
You should try to go to a previous version of your project and uninstall the service and for the future unistall the service each time you're building your setup project.
This happened to me several times.
Run visual studio in debug mode, and in your properties of your service project goto tab build events in Post-Build event command line and add a -d so you can run your service without installing it, it will run as an console app
The LocalService account acts as a non-privileged user on the local computer, and presents anonymous credentials to any remote server. Use the other accounts with caution, as they run with higher privileges and increase your risk of attacks from malicious code.
If your service application will not install correctly, check to make sure that the ServiceName property for the service class is set to the same value as is shown in the installer for that service. The value must be the same in both instances in order for your service to install correctly.
NoteNote
You can also look at the installation logs to get feedback on the installation process.
You should also check to determine whether you have another service with the same name already installed. Service names must be unique for installation to succeed.
STUPIDITY PERSONIFIED
As i told i was deploying my service. So this published code had a "setup.exe" file to it. Now I had already created a setup project for the windows service , which i was using for the installing , uninstalling . And i din't know (These words needs courage !!) that msi file created is required to install the windows service. So as i got through the folder structure of the setup project. I came across the .msi file . And hence the problem got solved. The Main problem was :
Me not knowing to use setup projects
And I confused setup.exe with the published windows service with the setup of windows service.
I am really sorry for stealing bandwidth off your precious time. Thanks to all for the valuable inputs.

Categories