簡體   English   中英

調試Windows服務

[英]Debug a Windows Service

我創建了一個Windows服務,該服務每2分鍾向用戶(從db檢索到的列表)發送一封電子郵件通知。 我希望檢查Web服務是否一切正常,因此希望調試該服務。 完成應用程序的服務器設置后,該服務將安裝在SMC(services.msc)中。

我的問題:如何調試Windows服務?

不能看到從服務

  1. 任務管理器
  2. 附加到流程

我嘗試了以下鏈接

我還從http://msdn.microsoft.com/zh-cn/library/aa291232%28VS.71%29.aspx#vxtskdebuggerpermissionsdebuggingasystemservice提供了調試權

讓我知道是否需要任何輸入。

謝謝!

編輯: @maycil

我寫了您的If(!Debug)代碼,但由於我看不到Visual Studio的正確顏色,所以我想有些問題。

替代文字

static void Main()
{
#if (!DEBUG)


            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] { new Service1Component() };
            ServiceBase.Run(ServicesToRun);


#else
            Service1Component s = new Service1Component();
            s.StartProcess();
#endif
}

在Service1Companent.cs中嘗試此代碼

根據maycil的回答,您可以使用Environment.UserInteractive來檢查服務是否在連接了控制台的情況下運行。 這樣,它既可以用於服務,也可以作為控制台應用程序進行調試。

static void Main( string[] args )
{
    if( !Environment.UserInteractive )
    {
        var servicesToRun = new ServiceBase[] {new Service1Component()};
        ServiceBase.Run( servicesToRun );
    }
    else
    {
        var services = new Service1Component();
        services.Start()
        Console.WriteLine( "Press return to exit" );
        Console.ReadLine();
        services.Stop();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM