簡體   English   中英

ServiceAppliction中的OnStart不起作用

[英]OnStart in serviceAppliction don't work

我建立了一個可以編輯數據庫數據的服務,我想在服務啟動/或請求重新啟動並且無法捕獲OnStart時為connectionString運行Encrypt

我在做什么錯?

我的課

  public class Service1 : IService1
    {

        protected void OnStart(string[] args)
        {
            string stop="";
 //can't get here
 //here Encrypt the section.
 // section.SectionInformation.ProtectSection("DataProtectiond");
        }

        public string GetData(int value)
        {
            //my functions
        }
}

在調試服務時,很難捕獲OnStart方法。 您無法從Visual Studio運行服務,並且在生產環境中,必須先調用OnStart方法,然后才能附加調試器。 您可以做的是將以下代碼放入OnStart方法中:

System.Diagnostics.Debugger.Launch()

這將彈出一個窗口,您可以在其中選擇調試器。 如果您已經打開Visual Studio解決方案,則可以選擇此選項,調試器將附加到您的服務中。

要從Visual Studio中“運行服務”,可以在Main()中放入以下內容:

static void Main()
    {
        if (!Environment.UserInteractive)
        {
            // Startup as service. 
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new MyServices() 
            };
            ServiceBase.Run(ServicesToRun);
        }
        else
        {
           //Start things locally here, 
           //for example the same things you do in the OnStart() method of your service
        }
    }

現在您可以從Visual Studio中簡單地運行該程序,但是當您將其安裝在生產服務器上時,它也可以作為服務使用...

暫無
暫無

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

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