【问题标题】:how to debug Window service in visual studio 2010如何在 Visual Studio 2010 中调试 Window 服务
【发布时间】:2014-04-25 11:33:47
【问题描述】:

我创建了从数据库读取数据的窗口服务。但是,它不从数据库读取数据。我正在尝试调试但无法发生。我用eventviewer写了几个日志。但是日志没有写在eventviewer中。但是,所有代码都在Window窗体应用程序中工作,代码是

eventLog1.WriteEntry("GApps Sync is Collecting parameters from GUI seting", System.Diagnostics.EventLogEntryType.Information);
       var folderPath = Path.Combine(Environment.GetFolderPath(
         Environment.SpecialFolder.ApplicationData), "GAppsSync");
       if (!Directory.Exists(folderPath))
       {
           eventLog1.WriteEntry("Please stop service and Run GUI Tool set all the syn parameter", System.Diagnostics.EventLogEntryType.Error);          
       }
        path = Path.Combine(folderPath, "databaseFile.db3");
        FileInfo fileInfo = new FileInfo(path);
        if (fileInfo.Exists)
        {
            eventLog1.WriteEntry("GApps Sync is getting parameters from GUI tool", System.Diagnostics.EventLogEntryType.Information);
            try
            {
                using (SQLiteConnection con = new SQLiteConnection("data source=" + path))
                {
                    con.Open();
                    SQLiteCommand cmdSyncPara = new SQLiteCommand("Select SyncInterval, CRMSetting,GoogleSetting,SyncOption From Synchroniszation",con);
                    SQLiteDataReader dataReader = cmdSyncPara.ExecuteReader();
                    eventLog1.WriteEntry("GApps Sync is Reading database parameter:", System.Diagnostics.EventLogEntryType.Error);
                    while (dataReader.Read())
                    {
                        SyncInterval = dataReader.GetString(0);
                        eventLog1.WriteEntry("GApps Sync is getting parameters from GUI tool Syncinterval:" + SyncInterval, System.Diagnostics.EventLogEntryType.Information);
                        CRMSetting = dataReader.GetString(1);
                        eventLog1.WriteEntry("GApps Sync is getting parameters from GUI tool CRMSetting:" + CRMSetting, System.Diagnostics.EventLogEntryType.Information);

                        GoogleSetting = dataReader.GetString(2);
                        SyncOption = dataReader.GetString(3);
                    }
                    eventLog1.WriteEntry("GApps Sync got GUI sync Options and Sync Interval parameters", System.Diagnostics.EventLogEntryType.Information); 
                    SQLiteCommand cmdReadData = new SQLiteCommand("Select Enable, GmailId,GmailPassword,EmployeeAccount From SyncDataDetail",con);
                    SQLiteDataReader dataReaderDetail = cmdReadData.ExecuteReader();
                    while (dataReaderDetail.Read())
                    {
                        DataContainer dc = new DataContainer();
                        dc.Enable = bool.Parse(dataReaderDetail.GetString(0));
                        dc.EmailText = dataReaderDetail.GetString(1);
                        dc.Password = Decrypt(dataReaderDetail.GetString(2));
                        dc.EmployeeAccount = dataReaderDetail.GetString(3);
                        ItemCollection.Add(dc);
                    }
                    eventLog1.WriteEntry("GApps Sync got GUI Save Account Mapping ", System.Diagnostics.EventLogEntryType.Information); 
                }
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry("GApps Sync Failed to get GUI Save Account Mapping ", System.Diagnostics.EventLogEntryType.Error); 
            }
        }            
    }

【问题讨论】:

  • 我们应该如何帮助您进行这样的一般性描述?这就像“我的车虽然有所有轮子,但我正在启动引擎,但它却无法运行”......
  • 我添加了代码。它不写一些日志

标签: c# visual-studio-2010 window


【解决方案1】:

您应该将该进程附加到 Visual Studio 中的调试器。 您可以从调试菜单中执行此操作。然后点击附加处理。 将显示一个新窗口,在此处选择进程并单击附加。 现在您处于 Windows 服务的调试模式。

来源:http://msdn.microsoft.com/en-us/library/7a50syb3(v=vs.110).aspx

【讨论】:

    【解决方案2】:

    调试 Windows 服务的最佳方法是像这样编辑您的 program.cs 文件

    static class Program { 
        static void Main()  { 
    
            #if DEBUG ServiceName myService = new ServiceName();
                myService.onDebug(); 
            #else ServiceBase[] ServicesToRun;
    
                ServicesToRun = new ServiceBase[] 
                {  
                    new ServiceName() 
                }; 
    
                ServiceBase.Run(ServicesToRun); 
    
            #endif
        } 
    }
    

    然后在调试模式下运行文件,在你想要的地方添加断点。 :)

    【讨论】:

      【解决方案3】:

      来自 MSDN (Debug Windows Service)

      向您的服务添加一个运行 OnStart 和 OnStop 方法的方法:

      internal void TestStartupAndStop(string[] args)
      {
          this.OnStart(args);
          Console.ReadLine();
          this.OnStop();
      }
      

      重写Main方法如下:

      static void Main(string[] args)
              {
                  if (Environment.UserInteractive)
                  {
                      MyNewService service1 = new MyNewService(args);
                      service1.TestStartupAndStop(args);
                  }
                  else
                  {
                      // Put the body of your old Main method here.
                  }
             }
      

      注意:您的服务还需要另一个构造函数,即:

      public Service1(string[] args)
      

      如果你想传递参数。

      在项目属性的Application选项卡中,将Output type设置为Console Application。(不要忘记这一步!)

      选择开始调试 (F5)。

      这会弹出一个控制台窗口,运行onStart,耐心等待你在控制台窗口中敲击一个键结束你的服务。

      要再次将程序作为 Windows 服务运行,请安装它并像往常一样为 Windows 服务启动它。没有必要撤消这些更改。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-10
        • 1970-01-01
        • 2011-06-08
        • 2018-09-07
        • 2011-10-12
        • 2011-07-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多