1

.net Framework 4、Visual Studio 2010 で 1 つの Windows Azure プロジェクトを作成しました。

Windows Azure SDK のバージョンは 1.8 です。

毎日のスケジュールされたタスクの実行を完了するために、1 つの 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() をテストする方法がわかりません。診断コードを試しましたが、それだけではありません。

誰でもこれについて私を助けてもらえますか?

ありがとう。

4

0 に答える 0