Windows Service中关于WCF也许MessageQueue的调试

Windows Service中关于WCF或者MessageQueue的调试

一般来讲,WCF或者MessageQueue都会启动一个后台线程来接收消息。如果我们通过Main方法来调试Windows Service的时候,一瞬间就会结束,所以我们应该使Main方法所在的线程处于等待状态,这样后台的WCF线程才会启动。所以我们可以在Main方法的最后加一个Console.ReadLine(), 但是我们这时候还需要做的就是把Project的属性从Windows Application改为Console Application,这样那句代码才能起作用。


还有,就是当停止一个Windows Service的时候,如果出现错误,想调试,该怎么调试?怎么调用OnStop方法?可以在Main方法中,设置从控制台接受传过来的字符串,如果是stop,那么就调用OnStop方法,然后跟进去调试。


        static void Main(string[] args)
        {
            try
            {
                Log.Info("The service is starting.");
                StartInvoke();
                Log.Info("The service has been stoped.");
            }
            catch (System.Exception ex)
            {

            }
        }

        private static void StartInvoke()
        {
            SubscriptionService serviceManager = new SubscriptionService();
            serviceManager.Start();
            if (Console.ReadLine() == "stop")
            {
                serviceManager.Stop();
            }
        }