Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
66 views
in Technique[技术] by (71.8m points)

c# - Debug Windows Service

Scenario

I've got a windows service written in C#. I've read all the google threads on how to debug it, but I still can't get it to work. I've run "PathTo.NetFrameworkInstallUtil.exe C:MyService.exe". It said the install was successful, however when I run "Services.msc", The service isn't displayed at all, anywhere. If I go into Task Manager, there is a process called "MyService.vshost.exe". Pretty sure that's not it, because it's a service, not a process.

Can Someone Explain To Me?

If I am supposed to see the service when I run Services.msc? (Bearing in mind this is all being done on a local machine, with no servers AT ALL.

Other

I'm running VS2008.

EDIT:

This is all being done on my local machine, I have no servers or access to any. Also, I don't even know what the service does, I want to debug it so I can walkthrough the code and see how it all works (the code inside the service, not the service itself - for any of you smarty pants that might suggest I look at a template).

EDIT 2:

NONE OF THESE ARE WORKING! Everytime I try something I get some message about having to use NET START or install the service.

EDIT 3:

I'm running VS2008.

I typed this: C:WINDOWSMicrosoft.NETFrameworkv2.0.50727InstallUtil.exe C:devRestarterinReleaseRestarter.exe

I got this: Microsoft (R) .NET Framework Installation utility Version 2.0.50727.3053 Copyright (c) Microsoft Corporation. All rights reserved.

Running a transacted installation.

Beginning the Install phase of the installation. See the contents of the log file for the C:devRestarterin ReleaseRestarter.exe assembly's progress. The file is located at C:devRestarterinReleaseEDT.Restar ter.InstallLog. Installing assembly 'C:devRestarterinReleaseRestarter.exe'. Affected parameters are: logtoconsole = assemblypath = C:devRestarterinReleaseRestarter.exe logfile = C:devRestarterinReleaseRestarter.InstallLog

The Install phase completed successfully, and the Commit phase is beginning. See the contents of the log file for the C:devRestarterin ReleaseRestarter.exe assembly's progress. The file is located at C:devRestarterinReleaseRestar ter.InstallLog. Committing assembly 'C:devRestarterinReleaseRestarter.exe'. Affected parameters are: logtoconsole = assemblypath = C:devRestarterinReleaseRestarter.exe logfile = C:devRestarterinReleaseRestarter.InstallLog

The Commit phase completed successfully.

The transacted install has completed.

C:Program FilesMicrosoft Visual Studio 9.0VC>

I then went to RUN -> Services.msc I can see nothing in there.

There is a process in Task Manager called "Restarter.vshost.exe".

That's it.

I only wanted to install and debug it. I know it works (as it it runs and doesn't crash). But the code was written by a friend and I want to understand the underlying code by walking through it in debug mode.

question from:https://stackoverflow.com/questions/2629720/debug-windows-service

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I recommend following pattern for debug:

 var ServiceToRun = new SomeService(); 
 if (Environment.UserInteractive)
 {
    // This used to run the service as a console (development phase only)

    ServiceToRun.Start();

    Console.WriteLine("Press Enter to terminate ...");
    Console.ReadLine();

    ServiceToRun.DoStop();
 }
 else
 {
    ServiceBase.Run(ServiceToRun);
 }

Edit: make sure that your target is Console Application, not Windows Application, otherwise it will not work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...