因此,我已经在程序中创建了服务。我需要此服务来更新我的表。它应该像检查短信的送达报告一样工作。
因此,在此服务中,我想从sms网关执行api以获取数据,然后将其插入SQL的Log表中。
我设法在服务中创建计时器,并更新了一些eventLog。因此,我的服务仍然有效,但是问题是,当我尝试使用sql服务器执行任何操作时,它可能什么也不执行,而且我真的不知道为什么。
服务:
public partial class Service1 : ServiceBase
{
private int eventId = 1;
DataSet ds = new DataSet();
SqlConnection cs = new SqlConnection("Data Source=lvrabel; Initial Catalog=EagleTest; Integrated Security=TRUE;");
SqlDataAdapter da = new SqlDataAdapter();
public Service1()
{
InitializeComponent();
eventLog1 = new EventLog();
if (!EventLog.SourceExists("MySource"))
{
EventLog.CreateEventSource("MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
Timer timer = new Timer();
timer.Interval = 60000; // 60 seconds
timer.Elapsed += new ElapsedEventHandler(this.GetReports);
timer.Start();
}
protected override void OnStop()
{
eventLog1.WriteEntry("In OnStop.");
}
public void OnTimer(object sender, ElapsedEventArgs args)
{
// TODO: Insert monitoring activities here.
// eventLog1.WriteEntry("Monitoring the System", EventLogEntryType.Information, eventId++);
}
public void GetReports(object sender, ElapsedEventArgs args)
{
//SqlCommand selectTelephone = new SqlCommand("select telephone from Contacts where LASTNAME = 'test'", cs);
//cs.Open();
//SqlDataAdapter da = new SqlDataAdapter(selectTelephone);
//DataTable dt = new DataTable();
//da.Fill(dt);
//foreach (DataRow dr in dt.Rows)
//{
// eventLog1.WriteEntry(dr["telephone"].ToString(), EventLogEntryType.Information, eventId++);
//}
//cs.Close();
cs.Open();
da.InsertCommand = new SqlCommand("insert into Log(IDContacts, IDEvent, Description, smsID) VALUES('5', '3', 'SMS OUT', '123')", cs); // dodelat id contacts
da.InsertCommand.ExecuteNonQuery();
cs.Close();
}
}
是否有可能如何对服务进行故障排除,或者您在代码中看到任何错误? (自动取款机我只有insertCommand。稍后我将把http api添加到代码中)
https://imgur.com/a/O5T78E2-我的安装程序正在使用中的imgur图像
添加的解决方案:
因此,所有问题都出在用户特权上。我使用的是本地系统服务,而不是网络服务。现在一切正常。
答案 0 :(得分:1)
如果您对系统具有足够的权限,则可以在服务运行时主动对其进行调试,以查看可能发生的异常。您可以参考已接受的答案here,也可以在OnStart()
回调中简单地拨打以下电话:
System.Diagnostics.Debugger.Break();
启动服务时,您将得到一个提示,指示发生了未处理的异常。
单击“是”选项,对UAC提示回答“是”,选择要使用的Visual Studio实例,然后正常进行调试。希望这将使您对发生的事情有更清晰的了解,而不必猜测。
HTH