views:

28

answers:

0

Hi Everyone,

I am trying to debug a windows service using the Debugger.Break() method.On the service OnStart() method i am calling my business logic(QueueProcessor.StartProcessing() ) to process the message queue items every 1sec.I have kept my breakpoint(Debugger.Break())in the startprocessing method. Now When i starting the service the Visual Studio Just In Time Debugger is popping up but when i click to debug the service nothing happens and after some time following message pops up "A fatal error has occurred and debugging needs to be terminated. For more details, please see the Microsoft Help and Support web site. HRESULT=0x80131c08. ErrorCode=0x0" Basically I am not able to debug my windows service.

Follwing the code in my Program.cs file which is used to load the service.

static void Main() {

        //#if DEBUG
        //System.Diagnostics.Debugger.Launch();
        //#endif
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
            { 
                new TravelMailService() 
            };
        ServiceBase.Run(ServicesToRun);
    }

On Service OnStart follwing thigs occurs.

public partial class TravelMailService : ServiceBase { Timer timer = new Timer(); public TravelMailService() { InitializeComponent(); }

    protected override void OnStart(string[] args)
    {
        timer.Interval = 1000;
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        timer.AutoReset = true;
        timer.Enabled = true;
        timer.Start();
    }

    void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        QueueProcessor.StartProcessing();                   //This is the business logic where i have kept my breakpoint
    }

    protected override void OnStop()
    {
        QueueProcessor.StopProcessing();
        timer.AutoReset = false;
        timer.Enabled = false;   
    }

}

And Finally this is my business logic which service is calling.

public class QueueProcessor { private static MessageQueue msgQ = null; private static object lockObject = new object();

    public static void StartProcessing()
    {
        Debugger.Break();                       //Code not breaking here.
        string queuePath = @".\private$\websiteemails";
        InsureQueueExists(queuePath);
        msgQ = new MessageQueue(queuePath);
        //msgQ.Formatter = new BinaryMessageFormatter();
        msgQ.MessageReadPropertyFilter.SetAll();
        msgQ.ReceiveCompleted += new ReceiveCompletedEventHandler(msgQ_ReceiveCompleted);
        msgQ.BeginReceive();
        // while (Console.ReadKey().Key != ConsoleKey.Q)
        //{
        //    // Press q to exit.
        //     Thread.Sleep(0);
        //}
    }

    static void msgQ_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
    {
        lock (lockObject)
        {
            EmailMessage msg = (EmailMessage)e.Message.Body;
            MailHelper.SendMailMessage(msg.from, msg.to, null, null, msg.subject, msg.message, null);
        }
        // Listen for the next message.
        msgQ.BeginReceive();
    }

}

An early answer is higly valued. Thanks in advance.