我在 .net framework 4、visual studio 2010 中创建了一个 Windows Azure 项目。
Windows Azure SDK 版本是 1.8。
我创建了一个 Windows 服务来完成日常计划任务的运行。
下面是代码 -
public partial class EdService1 : ServiceBase
{
public EdService1()
{
InitializeComponent();
}
#region On Start
protected override void OnStart(string[] args)
{
//System.Diagnostics.Debugger.Break();
//Thread thread = new Thread(new ThreadStart(ServiceStart));
//thread.IsBackground = true;
//thread.Name = "ThreadServiceStart";
//thread.Start();
#region Email Utility timer set and call
try
{
TraceService("start service");
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(this.ServiceTimer_Tick);
this.timer.Interval = 300000;//300000; //60000 millisecond = 1 minute
timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
ServiceTimer_Tick(null, null);
#endregion
#region Daily Utility timer set and call
Dailytimer = new System.Timers.Timer();
Dailytimer.Elapsed += new ElapsedEventHandler(this.DailyTimer_Tick);
this.Dailytimer.Interval = 7200000; //60000 millisecond = 1 minute
Dailytimer.AutoReset = true;
Dailytimer.Enabled = true;
Dailytimer.Start();
DailyTimer_Tick(null, null);
#endregion
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
#endregion
public void ServiceStart()
{
#region Email Utility timer set and call
TraceService("start service");
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(this.ServiceTimer_Tick);
this.timer.Interval = 300000;//300000; //60000 millisecond = 1 minute
timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
ServiceTimer_Tick(null, null);
#endregion
#region Daily Utility timer set and call
Dailytimer = new System.Timers.Timer();
Dailytimer.Elapsed += new ElapsedEventHandler(this.DailyTimer_Tick);
this.Dailytimer.Interval = 7200000; //60000 millisecond = 1 minute
Dailytimer.AutoReset = true;
Dailytimer.Enabled = true;
Dailytimer.Start();
DailyTimer_Tick(null, null);
#endregion
}
#region Sending Email Utility
private void ServiceTimer_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
strErrorMessage = string.Empty;
string strEmailError = "";
try
{
// calling functions here
}
catch (Exception ex)
{
strErrorMessage = ex.ToString();
}
if (strErrorMessage != "")
{
string strIPAddress = GetLocalIPAddress();
SendEmail("Error in Sending Email Utility " + strIPAddress, strErrorMessage.Replace("#", "\n </br>"), EmailFrom, EmailTo, ref strEmailError, EmailCC);
}
}
private void TraceService(string content)
{
//set up a filestream
FileStream fs = new FileStream(@"c:\ScheduledService.txt", FileMode.OpenOrCreate, FileAccess.Write);
//set up a streamwriter for adding text
StreamWriter sw = new StreamWriter(fs);
//find the end of the underlying filestream
sw.BaseStream.Seek(0, SeekOrigin.End);
//add the text
sw.WriteLine(content);
//add the text to the underlying filestream
sw.Flush();
//close the writer
sw.Close();
}
protected override void OnStop()
{
timer.AutoReset = false;
timer.Enabled = false;
TraceService("stopping service");
}
我尝试使用本地和管理员帐户启动此服务。但是得到这个提到的错误信息。
即使我不知道如何为这个服务测试这个方法 onStart() 。我尝试了诊断代码,但它并没有就此停止。
谁能帮我解决这个问题?
谢谢。