C#: Running and Debugging a Windows Service - c#

I am currently building a Windows Service with C# and I just can't figure out how to debug it.
I am building it and then registering with installutil:
> installutil MusicRepo_FileMonitor.exe
The Commit phase completed successfully.
The transacted install has completed.
And as you can see above, it is supposedly being registered successfully, but I can't find it in services.smc. I have also tried starting the service with net start [ServiceName] but it's telling me The service name is invalid. so I guess it's not finding it.
For the record, I'm on Vista and I'm developing the ws with .Net 3.5
[Update]
Solution

Try Task Manager and see if you see MusicRepo_FileMonitor.exe. If not, then the service is definitely not running.
Alternatively, in Visual Studio, Debug > Attach to Process, check show processes from all users, and see if its there.

Upon further research, I realized that I had no Installer attached to the service, so the problem was now fixed by adding an Installer.
Here is the tutorial I used to add a Service Installer and so on.

Related

Service cannot be started. The service process could not connect to the service controller when start service [duplicate]

Hi I am trying to write just a quick demo service. I am following the tutorial from MSDN here:
http://msdn.microsoft.com/en-us/library/zt39148a.aspx
So this tutorial basically sets up an basic service that writes to the event log. It also configures the installation and adds a setup project. So I have tried the installation on a couple of computers, one running Server 2008 R2 and one running Windows 7.
Basically what happens is the setup runs fine, but the service does not show up in the service list. I also checked the event log and receive an error with a description of:
"The service process could not connect to the service controller"
However there is no other information about the error. Does anyone have any idea how to get the service to show up in the service list and to run? Thanks.
One other item I could mention is that the custom log for the service is created however there are no entries.
The message "Service cannot be started. The service process could not connect to the service controller" is logged in the event log everytime you attempt to run a windows service from Visual Studio. Unlike most projects you create in Visual Studio, Windows Service projects cannot be run directly from the development environment by pressing F5. See the following msdn link http://msdn.microsoft.com/en-us/library/sd8zc8ha.aspx
Problem solved. I didn't follow the last part of the tutorial for adding a custom action :) I thought at first it was optional however it appears that is the final part of installing the service. Works perfect now.
You need to add an Installer to your service.
Go to the designer view of your service. click on the "Add Installer" link. This adds objects that are mandatory for the service installation.
Last thing is to make sure you run your installutil.exe with Administrator privileges.
Just uncheck "Enable the Visual Studio hosting process" works for me!

Install a service created with VS2010

I've to create a service in Visual Studio for one customer.
The customer IT service indicate me that they don't want any installer, only one exe that can be installed with instsrv.exe.
So I created a Service project on VS2010, compilated it(in 64 bits, it's an application that has to be ran in 64bits).
First problem: I didn't found any instsrv.exe, so I used InstallUtil.exe, the 64 bits version :
C:\Windows\Microsoft.Net\Framework64\v4.0.xxxx\InstallUtil.exe "PathToMyGeneratedExe"
It says me that "The commit phase completed successfully." and "The transacted install has completed.".
So I assume that the install completed successfully.
But if I go in the service console, I can't find the created service.
So what am I missing?
InstallUtil is intended for usage with a service installer (which is really easy to make, but a bit buried) I imagine your client wasnt after a gui installer but I think it may be worth your while putting in the base serviceinstaller.
Normally InstallUtil will also prompt for a user to run the service under as well. if you havent got this you probably arent installed
Take a read of http://arcanecode.com/2007/05/23/windows-services-in-c-adding-the-installer-part-3/ for details of how to do this
(TLDR right click on your service design surface and add installer)

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.

Windows Service in C# works on dev machine, crashes in test environment

I have been working on an application using .NET 4 and c# in Visual Studio 2010 Express. It's purpose is to watch a directory and import any documents it finds into our imaging system.
I included an installer class which provides the information needed by installutil.exe.
So to install locally, I build the solution, and then at command line:
installutil.exe MyProjectExecutable.exe
Which installs it as a service. This works fine in my development environment.
To deploy to the test environment, I use the same method:
installutil.exe MyProjectExecutable.exe.deploy
Except of course the executable has a second extension, .deploy. This is the way the application is published by Visual Studio. Everything works up tot his point
Here is the problem: once I have done this, I run the service and it starts properly, as indicated in the log file. However, once it detects files in the directory, the service will not do anything with them and will eventually crash. The only way I can tell it has crashed is to look in the windows Event Viewer. It will show the message:
"An unhandled win32 exception occurred in MyProjectExecutable.exe.deploy [4108]. Just-In-Time debugging this exception failed with the following error: Debugger could not be started because no user is logged on."
Two questions:
1) What could be causing this
2) Am I going through the correct steps to install a windows service, or is there a better way?
Both environments have .NET 4 installed.
In both environments I am placing the same documents in the import directory.
In both environments the service runs under the Local System account.
If my question reflects a lack of C# experience, my apologies in advance.
subscribe to AppDomain.UnhandledException Event
Are you watching a directory under 'Documents and Settings?' You mention that you're running under Local System, but is that in the C# code or in the Services Panel?
Start->Run
services.msc
< Find your App >
Right-click, Properties
Log On Tab
Log On As
This Account
< Plug in a user / pass with permissions >
In my experience this it is always a security problem. Check the rights that the service running user has. Can it access the folders you are monitoring and make the needed changes?
I have found the problem. in the config file, the listen directory was "c" whereas it should have been "C". Until now, I didn't think Windows was case-sensitive. Thanks for the help guys.

C# - Windows Service installer fails in custom action

We install a windows service from a custom action as described here: CodeProject
Every so often, when uninstalling our application, it does not properly uninstall the service. Instead it "marks the service for deletion".
You can see this message by running installutil.exe /u on our executable, when it is supposedly uninstalled.
Has anyone seen this issue or know a workaround? If a service is somehow installed with the same name as ours, we'd like to replace it. Instead the msi setup fails, giving a message saying our service is already installed.
Is there a way to do it without making a custom action to first uninstall any existing instances of our service's name before running the install custom action?
Is the service control panel (services.msc) open while you uninstall? This prevents your service from correctly being uninstalled.
If nothing else helps a reboot might be required as recommended in this article (though originally applying to .NET 1.1).
Check, if your service correctly stops additional thread(s) started from the service.

Categories