Friday, June 12, 2009

Debugging Windows Services

Hey Guys!

This post is some info. about windows services. There are lot of blog posts/tutorials floating around in the web that deals with creation of windows services. But this post deals with the methods to debug a windows service. After you start your service you can attach it to a debugger, so that you can debug a windows service! Conventionally it wouldn't be possible to debug a windows service by pressing F5, as you would have known. This is because services are run from the context of a services control manager.

Now let me stop these stories and get directly in to reality!!

In a nutshell, these are the steps to be followed:

* Develop the service
* Start the service
- Start / Control Panel / Administrative Services / Computer Management
- Click on Services and Applications / Services
- Scroll to your service
- Start it (Right click - start)
* Open your project for this service in VS.Net
* Set breakpoints (Obviously you cannot debug the onStart method or the Main method!)
* Choose Debug -> Processes
* Choose the windows service in the list of "Available Processes"
* Click on "Attach"

There you go!! Hope this helps somebody!!!

Want a "professional" view of the same? Refer to,

http://msdn.microsoft.com/en-us/library/7a50syb3(VS.80).aspx

Happy coding :)

Friday, June 5, 2009

Getting the Type of the current instance and Name of the method currently being executed

Hi Folks,

I am back, after a long time :) This post describes how we could get the name of the instance that is currently being executed and also the method name that is currently being executed.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace TestProject
{
class ToCall
{
public ToCall()
{
Console.WriteLine("This is " + this.GetType());
MethodBase method = MethodInfo.GetCurrentMethod();
string methodname = method.DeclaringType.FullName + "." + method.Name;
Console.WriteLine("Method name is " + methodname);

sample();
}

public void sample()
{
Console.WriteLine("This is " + this.GetType());
MethodBase method = MethodInfo.GetCurrentMethod();
string methodname = method.DeclaringType.FullName + "." + method.Name;
Console.WriteLine("Method name is " + methodname);
}
}

class Program
{
static void Main(string[] args)
{
ToCall d = new ToCall();
}
}
}


I will add some comments soon, just wanted to get started initially!!

Happy Coding :)