Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run(F5) windows service from visual studio

How to run a windows service project from visual studio.

I am building a windows serivce in visual studio 2008, I have to always run the service from control panel and then attach the debugger to running instance of the service. Its kind of annoying since I am cleaning a lot of code and need to restart my service many times during development.

I want to setup my project so as to be able to hit F5 and run the service and directly enter the debug mode. Some tips on how to achieve this would be great.

Thanks in advance!!!

like image 694
dotnetcoder Avatar asked Feb 12 '10 21:02

dotnetcoder


People also ask

How do I Debug Windows Service Project in Visual Studio?

Start Visual Studio with administrative credentials so you can attach to system processes. (Optional) On the Visual Studio menu bar, choose Tools, Options. In the Options dialog box, choose Debugging, Symbols, select the Microsoft Symbol Servers check box, and then choose the OK button.

How do you Debug or test your Windows Service without installing it in VB net?

msc and use Visual Studio > Debug > Attach to Process menu and attach to the Windows service. You can also consider using Thread. Sleep(10000) as the first line in the OnStart call, or Debugger. Break() to help you out to be able to attach before the service executes any work.


1 Answers

Copied from here.

static void Main(string[] args)   {       DemoService service = new DemoService();        if (Environment.UserInteractive)       {           service.OnStart(args);           Console.WriteLine("Press any key to stop program");           Console.Read();           service.OnStop();       }       else      {           ServiceBase.Run(service);       }   }   

This should allow you to run from within Visual Studio.

Another way would be to embed a programmatic breakpoint in your code by calling System.Diagnostics.Debugger.Break(). When you place this in, say, the OnStart() callback of your service and start your service from the Services console, the programmatic breakpoint will trigger a dialog box that allows you to attach to an existing instance of Visual Studio or to start a new instance. This is actually the mechanism I use to debug my service.

like image 97
Matt Davis Avatar answered Sep 18 '22 05:09

Matt Davis