我得到错误1053服务没有及时响应启动或控制请求

时间:2015-12-16 14:00:02

标签: c#

我正在尝试创建窗口服务,当文件夹中包含特定类型的文件时,该服务会向我发送邮件。运行服务时,我收到的错误如'错误1053-该服务未及时响应启动或控制请求。请帮我解决这个错误。' 我会更感谢你。

我编写的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.IO;
using System.ServiceProcess;
using System.Timers;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace DUP1
{
    class BrlCode
    {
        public void brlCount()
        {
            String[] files = Directory.GetFiles(@"H:\\EDI", "*.brl", SearchOption.AllDirectories);
            if (files.Length > 0)
            {
                var clientDomain = ConfigurationManager.AppSettings.Get("clientDomain");
                var from = ConfigurationManager.AppSettings.Get("from");
                var to = ConfigurationManager.AppSettings.Get("to");
                MailMessage mm = new MailMessage(from, to);
                mm.Body = ConfigurationManager.AppSettings.Get("body");
                mm.Subject = ConfigurationManager.AppSettings.Get("subject2");
                int leng = files.Length;
                for (int i = 0; i < files.Length; i++)
                {
                    String sep = "   |  ";

                    String Body1 = string.Join(sep, files, 0, leng);
                    mm.Body = Body1;

                }

                var smtp1 = new SmtpClient(ConfigurationManager.AppSettings.Get("clientDomain"));
                smtp1.Port = 25;

                //setting default credentials

                smtp1.Credentials = CredentialCache.DefaultNetworkCredentials;
                smtp1.EnableSsl = false;
                smtp1.Send(mm);

            }
        }
    }

}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.IO;
using System.ServiceProcess;
using System.Timers;

namespace DUP1
{
    public partial class Service1 : ServiceBase
    {
        private Timer timer1 = null;

        public Service1()
        {
            this.OnStart(null);
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer1 = new Timer();
            this.timer1.Interval = 540000;
            //Log("Service Started successfully");
            BrlCode bc = new BrlCode();

            bc.brlCount();
        }
        }


    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Mail;
using System.Configuration;
using System.IO;
using System.Timers;



namespace DUP1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 

                new Service1() 
            };
            ServiceBase.Run(ServicesToRun);
            Console.Write("yahoo");
            Console.ReadLine();
        }
    }
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>

  <system.web>
    <authorization>
      <allow users="dipsk4@gmail.com" />

    </authorization>
  </system.web>
  <appSettings>

    <add key="clientDomain" value="mail.allstate.com"/>
    <add key="directory" value="H:\\"/>
    <add key="from"  value="dipsk4@gmail.com"/>
    <add key="to"  value="dipsk4@gmail.com"/>
    <add key="body"  value="01 .lnr files has been received"/>
    <add key="subject1" value="LNR files"/>
    <add key="subject2" value="BRL files"/>
    <add key="subject3" value="DUP files"/>
    <add key="attachment"  value="H:\\deep.txt"/>
  </appSettings>
</configuration>

1 个答案:

答案 0 :(得分:0)

您应该尝试在不同的线程中卸载BrlCode完成的工作,以便OnStart尽可能快地返回。

protected override void OnStart(string[] args)
{
  timer1 = new Timer();
  this.timer1.Interval = 540000;

  //Log("Service Started successfully");

  Task.Run(() => 
  {
    BrlCode bc = new BrlCode();
    bc.brlCount();
  });
}