好吧,我讨厌被难倒但是这次我。我能够让服务安装得很好,所以工作得很好。当我启动服务时,它会停止,我没有得到有关错误的详细信息。所以这是我的代码:
using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.ServiceProcess;
using System.Text.RegularExpressions;
using System.Threading;
namespace MyServiceImport
{
public partial class MyServiceData : ServiceBase
{
private System.Timers.Timer _timer;
private Double ReadTime = Convert.ToDouble(ConfigurationManager.AppSettings.Get("ReadTime")) * 1000 * 60;
private DateTime _lastRun = DateTime.Now;
Thread _thread;
public MyServiceData()
{
InitializeComponent();
}
// These lines run when the service starts up
protected override void OnStart(string[] args)
{
_timer = new System.Timers.Timer();
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Enabled = true;
_timer.Interval = ReadTime;
_timer.Start();
#region Write to Audit Log
string Event = "Update Service";
string DoneBy = "Automation User";
string Information = "Update Service has Started.";
string MyTimeStamp = DateTime.Now.ToString();
SqlConnection conn = new SqlConnection(DBUtils.MyService_DBString);
string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
SqlCommand WriteLog = new SqlCommand(writeit, conn);
conn.Open();
WriteLog.Parameters.AddWithValue("Event", Event);
WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
WriteLog.Parameters.AddWithValue("Information", Information);
WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
WriteLog.ExecuteNonQuery();
conn.Close();
#endregion
}
// writes message into the activity log that the service has stopped.
protected override void OnStop()
{
#region Write to Audit Log
string Event = "Update Service";
string DoneBy = "Automation User";
string Information = "Update Service has Stopped.";
string MyTimeStamp = DateTime.Now.ToString();
SqlConnection conn = new SqlConnection(DBUtils.MyService_DBString);
string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
SqlCommand WriteLog = new SqlCommand(writeit, conn);
conn.Open();
WriteLog.Parameters.AddWithValue("Event", Event);
WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
WriteLog.Parameters.AddWithValue("Information", Information);
WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
WriteLog.ExecuteNonQuery();
conn.Close();
#endregion
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// set up connection string
string _csMyServiceDD = DBUtils.MyService_DBString;
string DoImportNow = string.Empty, TaskDay = string.Empty, TaskStartTime = string.Empty, TaskEndTime = string.Empty;
SqlConnection conn = new SqlConnection(_csMyServiceDD);
// Get task variables from database for comparison
string query = "SELECT TaskDay, TaskStartTime, TaskEndTime FROM MyService_TaskSchedule where TaskName = 'DoImport'";
SqlCommand cmd1 = new SqlCommand(query, conn);
// opens the connection to read
conn.Open();
SqlDataReader rdr1 = cmd1.ExecuteReader();
while (rdr1.Read())
{
TaskDay = rdr1.GetValue(0).ToString();
TaskStartTime = rdr1.GetValue(1).ToString();
TaskEndTime = rdr1.GetValue(2).ToString();
}
// closing the connection
conn.Close();
// setting _lastRun variable to this moment.
_lastRun = DateTime.Now;
// stop the timer
_timer.Stop();
try
{
// check if the current time is within the time range for this service task.
if (DateTime.Now.DayOfWeek.ToString() == TaskDay && DateTime.Now > Convert.ToDateTime(TaskStartTime) && DateTime.Now < Convert.ToDateTime(TaskEndTime))
{
_thread = new Thread(new ThreadStart(ImportData));
_thread.Name = "ImportData";
_thread.Start();
}
// decide if there is something to do
#region Write to Audit Log
string Event = "Update Service";
string DoneBy = "Automation User";
string Information = "Update Service has started importing data.";
string MyTimeStamp = DateTime.Now.ToString();
string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
SqlCommand WriteLog = new SqlCommand(writeit, conn);
conn.Open();
WriteLog.Parameters.AddWithValue("Event", Event);
WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
WriteLog.Parameters.AddWithValue("Information", Information);
WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
WriteLog.ExecuteNonQuery();
conn.Close();
#endregion
}
catch (Exception ex)
{
// Catch error and write to audit log
#region Write to Audit Log
string Event = "Update Service";
string DoneBy = "Automation User";
string Information = "Error occurred in Import - Message: " + ex.ToString() + "";
string MyTimeStamp = DateTime.Now.ToString();
string writeit = "INSERT INTO MyService_Activities (Event, DoneBy, Information, TimeStamp) values(@Event, @DoneBy, @Information, @TimeStamp)";
SqlCommand WriteLog = new SqlCommand(writeit, conn);
conn.Open();
WriteLog.Parameters.AddWithValue("Event", Event);
WriteLog.Parameters.AddWithValue("DoneBy", DoneBy);
WriteLog.Parameters.AddWithValue("Information", Information);
WriteLog.Parameters.AddWithValue("TimeStamp", MyTimeStamp);
WriteLog.ExecuteNonQuery();
conn.Close();
#endregion
}
_timer.Start();
}
答案 0 :(得分:0)
你确定你的捕捉{}按预期工作吗?我怀疑你没有得到任何反馈,因为你的SQLConnection无法正常工作。尝试做一个更简单的日志记录来排除它。
答案 1 :(得分:0)
您的问题和标签不会让我们继续下去。你正在使用C#?而且,我假设是Visual Studio?
真正帮助我调试服务的是NLog。
你通过NuGet安装它,它很容易为初学者。 Here只是众多优秀教程中的一个。我写的是普通的.txt文件。
您基本上在整个代码中添加了大量日志注释,然后尝试启动您的服务。当它失败时,你打开日志.txt,看看最后写的是什么。您现在知道它在该日志行和下一个日志行之间失败了。
在您对NLog感到满意之后,您甚至可以在代码中放置一个全局的,未处理的错误捕获器,并将Exception
写入NLog,它将StackTrace写入您的日志并为您提供更详细的信息。您的NLog.config文件必须设置为将Exception
格式化为文本。
答案 2 :(得分:0)
我重写了整个事情,首先只是做一个基本的服务启动,然后我添加了我的代码。现在运作良好。感谢大家的提示和指示。