Windows Azure项目 - Windows服务错误1053服务未及时响应启动或控制请求

时间:2013-07-17 08:44:04

标签: azure windows-services

我在.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()。我尝试过诊断代码,但并不止于此。

有人可以帮我吗?

感谢。

0 个答案:

没有答案