简体   繁体   中英

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:

  1. A form that creates an instance of the service class
  2. New methods DoStart and DoStop in my service class, which are public and call the protected OnStart and OnStop methods
  3. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM