我已经创建了从数据库中读取数据的窗口服务。但是,它不会从数据库中读取数据。我正在尝试调试但是不可能发生。我使用eventviewer编写了几个日志。但是log不是在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);
}
}
}
答案 0 :(得分:1)
您应该将过程附加到visual studio中的调试器。 您可以从调试菜单执行此操作。然后单击“附加”进行处理。 将显示一个新窗口,选择该过程并单击附加。 现在,您处于Windows服务的调试模式。
来源:http://msdn.microsoft.com/en-us/library/7a50syb3(v=vs.110).aspx
答案 1 :(得分:0)
调试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
}
}
然后在调试模式下运行该文件,在您想要的位置添加断点。 :)
答案 2 :(得分:0)
来自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)
如果你想传递参数。
在项目属性的“应用程序”选项卡中,将“输出类型”设置为“控制台应用程序”。(不要忘记此步骤!)
选择开始调试(F5)。
这将弹出一个控制台窗口,运行onStart,耐心地等待你在控制台窗口中点击一个键来结束你的服务。
要再次将程序作为Windows服务运行,请安装它并像往常一样启动Windows服务。没有必要扭转这些变化。