How to debug a Windows service which stops immediately after it starts - c#

I currently found an issue that one of my projects, which is a windows service, stops immediately after it starts. It was not like this before.
Worse still, I am unable to attach the service to Visual Studio since it stopped immediately after it started.
My system is Windows server 2012 R2 and Visual Studio is 2013.
Is there any way for me to debug the service?

You can start the service directly under debugger. To achieve this you can either modify the service start command (sc config binPath=) or you can use GFlags to alter the service image to start under debugger. In both cases your debugger will be in the background and you'll need to then remote into it.
But usually you can solve the issue from dumps, if any. Often simply starting the service executable from the same account as the service will reveal the issue.
Finaly lets not forget the power of printf. Add logging and isolate the issue from the logs. Your service should be doing this anyway.

I have just found an answer in MSDN.
https://msdn.microsoft.com/en-us/library/cktt23yw.aspx
Add the following code:
System.Diagnostics.Debugger.Launch();
At the very beginning of OnStart method. Then run net start serviceName (administrative credential is needed)

Related

Cannot debug Service Fabric

I have a statesless Service Fabric service
I am deploying it within my application into a 1 node dev cluster
None of my breakpoints ever get hit
I have rebuilt, restarted, updated visual studio and I am now really out of ideas
Has anyone ever seen this?
The strange thing is that my breakpoints stay active
I know my logic is being executed as messages are shown in the console
Paul
Linking the answer in the comments
https://stackoverflow.com/a/42118286/5946937
Close all Visual Studio windows.
Restart the Service Fabric service: Type services.msc in the windows run tool. Find Microsoft Service Fabric Host Service. Right Click + Restart.
Reset the local cluster: Look at the Service Fabric Local Cluster Manager Icon in the notification area in the right side of the task bar. Right Click + Reset Local Cluster.
Open the problematic solution in Visual Studio.
Clean and Rebuild solution.
Finally run your solution (press F5).
There is one other thing that I noticed. After pressing an F5, on the process tab, keep an eye on the process id (this is just a sample reference, not from a service fabric process), in my case, there were no processes attached at all. In such a case, go to Debug -> Attach to Process -> Choose the process you need to debug.
One thing to keep in mind is that sometimes you may have to select the show process from all users checkbox because visual studio may be running in admin mode but the application process maybe running in normal user mode.
You can also understand that the right process has not been attached when you see something like this in front of the breakpoint. This is also an indication that the right process has not been attached. If the right process has been attached, the debugging should be successful!

Windows service fails to start

I'm trying to get my head around WCF applications hosted as a Windows service but I'm not sure what's happening here.
Basically, to get started, I've got a literal copy and paste of the code in this article: (I know, I shouldn't)
https://msdn.microsoft.com/en-us/library/vstudio/ms733069%28v=vs.110%29.aspx
The service builds fine and I can install it without issue. It shows up in services.msc also. I'm installing it as follows: (from the visual studio command prompt)
installutil Service.exe
However when it runs, I get the following error:
The WCFWindowsServiceSample service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.
What am I doing wrong?

Windows can't start service

I am developing an app to install & run as a Windows service. It's still buggy and stopped unexpectedly (leaving nothing in the Windows Event log either) and when I tried to start it up again I got the error
Windows could not start the {name of service} service on Local Computer.
Error 1067: The process terminated unexpectedly.
I restarted the computer (Server 2008 R2; a virtual machine, in case that makes a difference), no help. Uninstalled and reinstalled my service, and still get that error!
Something on the machine needs cleared out / refreshed so that it will attempt to start the service again. I don't want to rebuild the whole virtual machine but I'm out of ideas.
UPDATE
I forgot to mention that this Windows service -- exact same code & install -- is running on 3 other machines without the above error. Putting in that debugger in OnStart() still sounds like the way to go though.
If you have access to the code, you can add a method Debugger.Break at the first line of the OnStart method.
Once you "break" into the application investigate where the exception is thrown.
You can attach the debugger to a running service. Details at Debug Windows Service Applications.
It's fortunate that you can replicate the problem easily. You can install the .NET Studio with code to debug your problem.
Another technique is to make the Service into a regular app. You know that the suspect code is during Startup. So simply create the app with code in the Startup function, and start debugging from there.
Good luck,
Tommy Kwee

The service process could not connect to the service controller

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!

Debugging RESTful WCF Service

I am attempting to debug a RESTful WCF service I've created using a web browser as my client. Initially things go well however after stopping the debug session and making some changes to my code when I restart it's as if the old code is still being run. My breakpoints are ignored and it's as if a process is not being stopped.
I am running the service within Visual Studio and here's the URL I am asking the browser to retrieve:
http://localhost:25000/IntegrationService.svc/DeskQueue/tophd
It's clear the proper method is being called but the breakpoint I have at the top of the method is being ignored. Is there some external process I need to stop?
Thanks for any help.
Maybe it would be more convenient to deploy your service on IIS (Project settings->Web->Use Local IIS Web server, Create virtual directory) and then debug the service from Visual Studio (Debug->Attach to process->w3wp.exe).
You need to kill Cassini (the WebDev Server) as it's holding on to your older built WCF Server code....it doesn't automatically reload the newer DLL.
There are a few options:
You can manually stop Cassini by looking in your Notification area for the "ASP.Net Development Server" icon (yellow with world inside) and selecting Stop (via the context menu or Dialog).
You could install the Visual Studio addin KillCassini, so you just have to press a key combo to kill the server when you wanted it killed
http://visualstudiogallery.msdn.microsoft.com/1269c9a1-fcfe-4b47-91e7-22c7027f3c41
You could turn on Edit-and-Continue, so that it restarts a new Cassini the next time you Debug
Automatically stop/restart ASP.NET Development Server on Build
If you have some really exotic debug/test/run cycle then you could leverage a drop-in compatible replacement for Cassini that comes with source code.
http://cassinidev.codeplex.com/

Categories

Resources