如何在visualstudio2010中调试窗口服务
本文关键字:调试 窗口 服务 visualstudio2010 | 更新日期: 2023-09-27 18:00:00
我创建了从数据库读取数据的窗口服务。但是,它不会从数据库中读取数据。我正在尝试调试,但未能成功。我已经使用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);
}
}
}
您应该将进程附加到visualstudio中的调试器。您可以从调试菜单中执行此操作。然后单击"附加到进程"。将显示一个新窗口,在其中选择流程,然后单击"附加"。现在您正处于windows服务的调试模式。
来源:http://msdn.microsoft.com/en-us/library/7a50syb3(v=vs.110).aspx
调试windows服务的最佳方法编辑您的程序.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
}
}
然后在调试模式下运行该文件,在需要的地方添加断点。:)
来自MSDN(调试Windows服务)
向您的服务添加一个运行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)
如果你想传递参数。
在项目属性的"应用程序"选项卡中,将"输出"类型设置为"控制台应用程序"(不要忘记这一步!)
选择"开始调试"(F5)。
这将弹出一个控制台窗口,运行onStart,耐心等待您点击控制台窗口中的键来结束服务。
若要再次将程序作为Windows服务运行,请安装该程序,然后像往常一样启动Windows服务。没有必要扭转这些变化。