Windows Services in C#: Controlling Your Service from Visual Studio (part 5)

In part 3 of this series I documented how to use the Microsoft Management Console to control your service and view the event log. But did you know you can do it right inside Visual Studio?

Inside Visual Studio, open the Server Explorer (I keep mine docked over on the left). Under any database servers you may have should be your computer, click the + symbol to expand the tree.

[Pic of Server Explorer]

Now you can see quite a few items, including Services and Event Logs. Expand the services tree, and let’s scroll down to our service, ArcaneCodeTimeLogger. Right clicking will show us the various commands available to us. Since the service is already running, you can pause or stop it.

[Pic of Services in Server Explorer]

Having this functionality right within Visual Studio makes it very easy when it’s time to debug and test your various methods such as OnStart, OnStop, OnPause, etc. But wait, there’s more!

Just as with services, you can also examine the event log. Scroll up to the event log node and expand it. If you read my earlier series on event logging (http://shrinkster.com/p6d), you know I suggest creating your own distinct event log instead of shoving everything into the Application log. Now you can see why, it makes it very easy to pick out the messages for your app. Expand the two nodes for our service and you can see the first part of the messages appearing in the tree.

[Pic of Event Log in Server Explorer]

To see the complete message, simply double click on it. It will appear, along with other associated data, in the Properties window of Visual Studio.

[Pic of Properties showing detailed EventLog Message]

One thing you should note, when you use Visual Studio to debug your Windows Service, VS “helpfully” hides a lot of your windows, including the Server Explorer. You can get it back though, simply go to the View and pick the Server Explorer to make it appear again.

Now, you may be wondering why way back in part 3 we went through the MMC (Microsoft Management Console) instead of doing it this way. There are often multiple ways to accomplish tasks, and it’s often useful to know them all. For example, let’s say you have your service installed on a users PC and need to stop it or look at its events. If you don’t have Visual Studio installed on the box, what are you going to do?

When you do have VS, using the Server Explorer from within Visual Studio can make it easy to develop and debug your Windows Services. Take a few minutes to explore it’s capabilities, so you’ll have a second way to work with your services.