windows service cannot stop - c#

Created a simple WCF service that basically logs to a db.
the build is fine and created a msi install file for it.
I installed the windows service and start it which works, however when i stop it i get this error:
"Could not stop the service1 service
on local computer. The service did not
return an error. This could be an
internal windows error or an internal
service error. If the problem
persists, contact your system
administrator."

There are a couple of ways to diagnose this:
If you haven't already, separate out the implementation of the windows service from the actual service executable. That way you can run / debug the code from a console or forms app easily.
If that doesn't reveal any answers, add verbose logging to the shutdown method to find out what is happening.

Related

Windows services does not run my class method

I have a windows service application which would scan the wifi list and save to file. then i will read the file and write to mysql database and close the file. my wifi scanning and db methods in another class within same windows service project which i invoke in service class. I am able to run and install the windows service but i think those methods are not called. I have even tried them in the service class but they still didn't call up in OnStart method. When i run it section mode and debug from visual studio it works fine but when i install it in installutil it only starts the service nothing happens though the service still runs in the background. I've created windows form version of this and that works fine.
I think it's more likely that the class method is called but writing the output file fails. The account that is used to run the service likely has no rights to create and/or write the output file. Check the folder that it should be written to and see if the account that runs your service has proper access rights. You can also use Process Monitor to verify this (set a filter on Path or Process Name).

why does my service says it's running even though it's not?

I created a service using .NET that is supposed to start automatically and run as a User with my username & password.
If I restart my PC, log on with the same username & password, and go to Task Manager, it says that the service is running, but when I check the Event Log there are no messages stating that the service has started, and in fact, the service has not started, because it doesn't do what it's supposed to be doing.
On the other hand, if I manually stop and start the service, then the entries in the Event Log appear and the service runs as it's supposed to.
So when I restart my PC, how come Task Manager says that the service is running even though it's not running? Is there a security setting that I need to tweak?
Does your service have a dependency on another Windows service? If so then it may be that your service is failing to initialize correctly when the machine restarts as it can't make use of the functionality in needs from the dependent service.
This would explain why if you restart the service is works correct.
I was able to solve the problem: the service was failing to initialize properly on windows restart because it was not dependent on any service, when it should have been dependent on the event log service. Setting the "ServicesDependedOn" property of the ServiceInstaller object to "Windows Event Log" solved the problem. Thanks for the help!

Problems with starting windows service on windows xp SP3

I'm currently facing a problem which I can not resolve and I really don't know what to do anymore.
When I'm trying to start the service I receive the message: "The service is started but again also stopped, this because that some of the services will stop if they have nothing to do, for example the performance logs and the alerts service".
I've looked into the Windows Logs but nothing is written there which could describe why my service is all the time stopping. I've also tried to fire the windows service via the command prompt which gives me the message: "The service is not started, but the service didn't return any faults.".
I've tried to remove all keys which references to my service, which didn't resolve the issue.
I've searched on google (maybe not good enough) to find an answer but I didn't found any. I did found some websites which describes what I could do, but all of these suggestions didn't work.
This is kinda ** because I do not know where to look. I do not have any error message, i do not have any id which i can use to search on. I really don't know where to start and I hope you guys can help me on this one.
Detailed explanation about the windows service
OS: Windows XP SP3
.Net Framework: .Net 4.0 Client Profile
Language: C#
Development environment: Visual Studio 2010 Professional (but Visual Studio 2012 RC is installed)
Communications: WCF (Named Pipes), WCF (BasicHTTPBinding)
Named Pipes:
I have chosen for this solution because I wanted to communicate from a windows service to a windows form application. It worked now for quite some time but suddenly my windows service shuts it self down and I couldn't restart it anymore.
There are two named pipes services implemented: An event service which will send any notification to the windows form application and an management service which gives my windows form application the possibility to maintain my windows service.
BasicHTTPBinding:
The basic http binding makes the connection to a central server. This connection is then used for streaming information from the client to the server.
I do not know which additional information you will need, but if you guys need something then I'll try to give it as detailed as possible.
Thank you in advance.
Ok, I have solved the issue and really it is a stupid one. Windows XP will not throw any error or does not report to you that the book where all logs are stored is full!!! After cleaning the log my service started perfectly and keeps running. How stupid! :)

windows service working on windows 7 but not on windows server 2003

Solved the problem see the bottom of my post.
So I have a simple windows service that watches a specific folder and upploads files that come into it to a server using a web service.
It's working fine on my machine using windows 7 but when I try to start it on a windows server 2003, I receive an error: Error 1053: the service did not respond to start or control request in a timely fashion. But I get this message after only a few seconds.
I have created the ServicesPipeTimeout and set it to 60000 milliseconds.
I have tried running it from command line using sc query command and found out that the WIN32_EXIT_CODE is 0, which I think means that the service doesn´t even try to start because it find an error before it starts.
In the event viewer I get errors 7000 and 7009.
I am the Administrator on the windows server.
The only thing I haven´t tried is a hotfix I found from microsoft but I don´t want to use it because as I understand it, it is for when the service actually times out.http://support.microsoft.com/kb/886695
I have tried everything I can think of, is there anything that I am missing?
Gísli
EDIT: Re-installed the .NET framework and now I get a new error, Saying that the service controller can not be found.
EDIT: I setup the service with a setup project, not using the installutil command. This is because I need to get user input during the installation and save that in the registry.
EDIT: I have installed the .NET 4.0 framework, it wasn´t possible to install the service with out doing that.
In addition to what I wrote above I have also tried:
Rebooting.
Re-installing.
I have tried to change the permissions on the files that the service needs to access.
Changing permissions in the registry editor.
Edited the code so that the onStart function only starts one thread.
I think it is some kind of permission problem but I don´t have much experience dealing with Windows server.
Solution:
It turned out to be two seperate problems. The .NET framwork had to be repaired and I had to remove the try/catch clause that I had when starting the service. For some reason (unknown to me) the try catch block did something that made it impossible to start the service in a windows server 2003 but it ran fine on windows 7.
It would be very interesting to know why this is.
Thanks for all the help.
Gísli
Have you installed the right version of .NET Framework on the Server 2003 PC? What comes as standard on Windows 7 will need to be installed manually on an older OS.
You say "I have tried everything I can think of". Please edit the question to show what you have already tried so we don't suggest something you have already done.
EDIT:
Try also the Fusion Log viewer. Set it to log failures then start your service. Hit refresh then see if any errors are logged. Double-click a line for more details.
I'd guess that you installed the .NET 3.5 service using .NET 2.0? InstallUtil will work since the CLR is the same, but the service won't start because of .NET 3.5 dependencies.
See Also:
How to install a Windows service developed in .NET 3.5?
In your OnStart code you could wrap everything in a try-catch block and write the exception message and stack trace to a file using something like:
File.WriteAllText(#"C:\Temp\MyServiceLog.txt", exp.Message + exp.StackTrace);
This will help you analyze the problem. Also, I'm using a very simple way of debugging my services: I'm wrapping them in a WinForms wrapper if the session is interactive. For that, I need the following:
A form that creates an instance of the service class
New methods DoStart and DoStop in my service class, which are public and call the protected OnStart and OnStop methods
New code in above form's OnLoad and OnClose handlers, so the DoStart and DoStop methods of the service instance are called accordingly
Then I add the following code to the Main method in Program.cs:
if (Environment.UserInteractive)
{
Application.Run(new FormServiceHoster());
}
else
{
// ... old code to create service instance.
}
This way you're making the "service" run as an application when you just hit F5 in Visual Studio or double click the EXE in the Explorer. When the service is actually run as a service, the Forms-code is ignored.
Usually you can debug services by attaching to the process, however, that doesn't work for debugging the start and stop code. This method helps a great deal debugging the service start and stop code.
You will need to add references to System.Windows.Forms and System.Drawing manually.
This is the point where I bet you wish you had used some logging package like log4net, which might help tell how far your program gets.
Is it possible that the code in the OnStart event is actually taking longer than you expect. If so, you could move that code to a thread, so that the OnStart event finishes (and so Windows reckons the service has started) while your thread continues working.
Are you running the service as admin? If so, that usually rules out permissions issues. (If it still goes wrong, it will rule out the KB 886695 issue as that seems to only apply to local system).
Since you are using .NET 4.0 are you sure that either, the full version is installed, rather than the Client Profile, or that your application works with the client profile?

Strange Problem with a .NET Windows Service

I have two Windows services written in C# following the same patterns and methodology.
Both services were development tested against a Windows 7 VM and QA tested on Windows Server 2008 VM. Both services have been installed and uninstalled many times under these test environments without issue, however upon installing in the production environment (Windows Server 2008) one of the two services refuses to start.
To install the services we are using InstallUtil.exe with ServiceInstaller and ServiceProcessInstaller components attached to the service.
By all appearances, the failing service installs successfully. InstallUtil.exe reports success and the service appears in the Services snapin. You can also locate the service in the registry under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Blah Blah. However, if you attempt to start the service you get the following:
net start blah.blah.exe
"The service name is invalid."
...or going through the Services snapin...
"Windows could not start the "Blah Blah" service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion."
I have added some event logging to the constructor of the service class failing service, but it does not appear to get called.
As this is a production box, there is no Visual Studio on the box and remote debugging is out of the question.
Is there any other way for me to gain debugging info on why the failing service isn't starting?
Is there any other obvious-ish reason that I might see this kind of issue?
Edit: I should have also mentioned.. The only other evidence of a problem in the Windows Event Viewer is two messages in the System log from the Service Control Manager:
"A timeout was reached (30000 milliseconds) while waiting for the Blah Blah service to connect.
"The Blah Blah service failed to start due to the following error:
The service did not respond to the start or control request in a timely fashion."
Edit: Resolved
The issue ended up being a combination of a configuration mistake and a bug that was hiding it. See my answer below for more details.
Jeopardy Answer: "How might invalid custom configuration combined with a bad global exception handler manifest itself in a .NET Windows service?"
Figured it out.
The root cause of the problem was an invalid custom configuration section in the app.config. We use a custom configuration section to configure the service from the app.config and the assembly and namespace of the ConfigurationSection derived class had changed recently.
As it turns out, our production configuration was looking for the definition of the custom ConfigurationSection in the wrong assembly and the exception thrown when failing to instantiate it was getting hidden by a bug where exceptions caught early in the life of the service would attempt to be logged to the custom log rather than the Application event log. (Since the event log source did not exist on the custom event log, this would throw another exception from the global exception handler and the service would die in the constructor.)
This second exception did not get logged anywhere and we only found it through code inspection.
The resolution was to fix the configuration and to modify the global exception handler to only attempt to write to the Application event log using the service name as the event log source. (InstallUtil registers the service name as an event log source on the Application log.)
Thanks for the help everyone! Sorry this particular issue ended up being so specific to our setup.
It could be possible from the error message that you've described
net start blah.blah.exe "The service name is invalid."
That the name that you gave the service in the service install component that you added in visual studio is not what you think it is.
I've had this problem quite a few times with developers misnaming services in installs.
what are you trying to do on service start?
also check the account through which the service is running and the account has the necessary privileges.
I've run into problems like this many times while programming my own services, so I'll just list out a bunch of things that at various points solved my problems and hope that they help you:
I've had to restart services.msc because I've uninstalled a service it still thought it had a reference to. However, when I would start it, the "service was invalid"
If you're making the service from a console application (so that it can be debugged) but forget to change it back to a service, it won't start.
When I was using InstallUtil.exe, sometimes it would attempt to install multiple copies, so I switched to just using a Setup Project.
Hope that helps in some way.
This is a common issue. What code do you have in your Start event?
You should only have code that activates a Timer when the service starts. This allows the Start event to complete quickly and notify the controller. If the execution takes longer than that, you will get the error you received. There might be some reason (possibly data-related) why its taking longer in production.
When the Timer ticks, execute your code and stop the Timer. Obviously, also put everything in try/catch and log the exceptions.
It is possible for this error to occur when
your service is depending on another service/application
or due to invalid app.config configurations
Encountered the same issue when Entity Framework was unable to connect to the database server due to lack of permission.
Better to add a global level error logging mechanism to your application to debug the issue easily.Event Viewer might not reveal exact details in some scenarios.

Categories