Install windows service from the command line or debugger

How to: Install and uninstall Windows services

If you’re developing a Windows service with the .NET Framework, you can quickly install your service app by using the InstallUtil.exe command-line utility or PowerShell. Developers who want to release a Windows service that users can install and uninstall can use the free WiX Toolset or commercial tools like Advanced Installer, InstallShield, or others. For more information, see Create an installer package (Windows desktop).

If you want to uninstall a service from your computer, don’t follow the steps in this article. Instead, find out which program or software package installed the service, and then choose Apps in Settings to uninstall that program. Note that many services are integral parts of Windows; if you remove them, you might cause system instability.

To use the steps in this article, you first need to add a service installer to your Windows service. For more information, see Walkthrough: Creating a Windows service app.

You can’t run Windows service projects directly from the Visual Studio development environment by pressing F5. Before you can run the project, you must install the service in the project.

You can use Server Explorer to verify that you’ve installed or uninstalled your service.

Install using InstallUtil.exe utility

From the Start menu, select the Visual Studio directory, then select Developer Command Prompt for VS .

The Developer Command Prompt for Visual Studio appears.

Access the directory where your project’s compiled executable file is located.

Run InstallUtil.exe from the command prompt with your project’s executable as a parameter:

If you’re using the Developer Command Prompt for Visual Studio, InstallUtil.exe should be on the system path. Otherwise, you can add it to the path, or use the fully qualified path to invoke it. This tool is installed with the .NET Framework in %WINDIR%\Microsoft.NET\Framework[64]\ .

  • For the 32-bit version of the .NET Framework 4 or 4.5 and later, if your Windows installation directory is C:\Windows, the default path is C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe.
  • For the 64-bit version of the .NET Framework 4 or 4.5 and later, the default path is C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe.

Uninstall using InstallUtil.exe utility

From the Start menu, select the Visual Studio directory, then select Developer Command Prompt for VS .

Читайте также:  Microsoft silverlight для windows 10 что это

The Developer Command Prompt for Visual Studio appears.

Run InstallUtil.exe from the command prompt with your project’s output as a parameter:

After the executable for a service is deleted, the service might still be present in the registry. If that’s the case, use the command sc delete to remove the entry for the service from the registry.

Install using PowerShell

From the Start menu, select the Windows PowerShell directory, then select Windows PowerShell.

Access the directory where your project’s compiled executable file is located.

Run the New-Service cmdlet with the with your project’s output and a service name as parameters:

Uninstall using PowerShell

From the Start menu, select the Windows PowerShell directory, then select Windows PowerShell.

Run the Remove-Service cmdlet with the name of your service as parameter:

After the executable for a service is deleted, the service might still be present in the registry. If that’s the case, use the command sc delete to remove the entry for the service from the registry.

Message: WINDOWS SERVICE START FAILURE

Whenever log in through user profile, a message is displayed as:

«Cannot start service from the command line or a debugger. A Windows Service must be installed (using installutil.exe) and then started with the Server Explorer, Windows Services, Administrative tool or the NET START command«

I am not aware of the reason but the observation afterwards are

  1. System slows down whether you work online or offline.
  2. Would not allow easily to shut down or restart the system through start menu and have to use the power button ultimately.

Request to suggest the solution if any.

Replies (1) 

Windows Services are the programs that operates in the background which makes the Windows system whole. To resolve this issue, we suggest running a System File Checker scan and Deployment Image Servicing and Management comand-line utility. Here are the steps:

  1. Open the Start menu, type ‘cmd’, right-click on Command Prompt and select
  2. Enter the following command-line (press enter after each command-line):

DISM /Online /Cleanup-Image /CheckHealth
DISM /Online /Cleanup-Image /ScanHealth
DISM /Online /Cleanup-Image /RestoreHealth
sfc /scannow

Should the issue persists, please make sure that all available Windows updates are installed.

Install windows service from the command line or debugger

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Answered by:

Question

Answers

Do you mean you cannot start service from the command line?

Like following cmd,

Using net stop [service name] to stop it and net start [service name] to start it up again basically restarting the service.

Читайте также:  Windows 10 хот спот командная строка

If so, you should make sure you have done the thing correctly. If tha’t not what you want, please be more specific about what you are trying to do and feel free to let me know.

Firstly you should make sure your service code is working fine.

Secondly, if you are win7 or win8 os,

  1. On the Windows Start menu or Start screen, choose Visual Studio , Visual Studio Tools , Developer Command Prompt .
  2. A Visual Studio command prompt appears.
  3. Run InstallUtil.exe from the command prompt with your project’s output as a parameter:

Here is my screenshot,

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.

Debugging a Windows Service

I am making a Windows Service and I want to debug it.

This is the error I get when I try to debug it:

Cannot start service from the command line or a debugger. A Windows service must be first installed and then started with the Server Explorer, Windows Services Administrative TOll or the NET start command.

I have already installed my service using InstallUtil, but I am still facing problems.

Also, when I try to attach a process, my service goes into the running mode, it never starts debugging.

EDIT: DO we have to reinstall the Windows Service everytime we make a change or just building it would suffice?

9 Answers 9

In your OnStart use something like this:

For the most use cases it’s good enough to run the service as console application. To do this, I usually have the following startup code:

This makes it easy not only to run and debug the service, but it can then also install and uninstall without needing the InstallUtil program.

This question has an excellent answer in making the service a console/service hybrid. See the answer from user marc_s. I don’t want to duplicate the answer here.

I, personally for me, found the easiest solution is not change the code, by adding more mess and #if #else directives, but simply:

  1. Compile your service binaries in DEBUG mode
  2. Point installed service to DEBUG binaries
  3. Run service

Use connect to process dialog of VS to connect to your running process

The good thing on this that you don’t change the code so it’s exactly the same as your production binaries, which, I think, is kind of important.

One way that I’ve done it before was to insert a Debugger.Break() in the service on start method. Compile and install the service. When it starts it break and open the debug with dialog, from there you should be able to attach and debug.

The Debugger.Launch method is a good way but I prefer to create a class that does the processing and call it from the service, This can then also be called from a win forms app. eg:

then in your service / win forms app just create an instance of the processing class as a member variable and call the method on start and stop. It can be used in the service, or a win forms app with a start and stop button, which I find a lot quicker than attaching the debugger each time because you can set the windows application to start as default and add any breakpoints into the processing manager.

extract from service code:

EDIT: Personally, I have a console application in the same project that does all the work. I then just have the service run the Main of the console application. It makes debugging easy especially when just developing.

How do I debug Windows services in Visual Studio?

Is it possible to debug the Windows services in Visual Studio?

I used code like

but it is giving some code error like:

I got two event error: eventID 4096 VsJITDebugger and «The service did not respond to the start or control request in a timely fashion.»

17 Answers 17

Use the following code in service OnStart method:

Choose the Visual Studio option from the pop up message.

Note: To use it in only Debug mode, a #if DEBUG compiler directive can be used, as follows. This will prevent accidental or debugging in release mode on a production server.

You can also try this.

  1. Create your Windows service and install and start…. That is, Windows services must be running in your system.
  2. While your service is running, go to the Debug menu, click on Attach Process (or process in old Visual Studio)
  3. Find your running service, and then make sure the Show process from all users and Show processes in all sessions is selected, if not then select it.

  1. Click the Attach button
  2. Click OK
  3. Click Close
  4. Set a break point to your desirable location and wait for execute. It will debug automatic whenever your code reaches to that point.
  5. Remember, put your breakpoint at reachable place, if it is onStart(), then stop and start the service again

(After a lot of googling, I found this in «How to debug the Windows Services in Visual Studio».)

Читайте также:  Что дает активированная windows 10
Оцените статью