【问题标题】:Im getting Error 1053 the service did not respond to the start or control request in a timely fashion我收到错误 1053 服务没有及时响应启动或控制请求
【发布时间】:2016-03-22 16:57:39
【问题描述】:

我正在尝试创建窗口服务,当文件夹中出现特定类型的文件时,它会向我发送邮件。在运行该服务时,我收到类似 'Error 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>

【问题讨论】:

标签: c#


【解决方案1】:

您应该尝试在不同的线程中卸载 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();
  });
}

【讨论】:

  • 你的意思是它不工作?您仍然收到错误消息吗?
  • 现在该错误没有出现,但服务未按预期提供任何输出为“未发送邮件”。
  • 所以这是进步:) 您关于错误 1053 的问题的答案已得到解答。
  • 仍然无法发送邮件。请有人建议我的代码中的错误,为什么它没有发送邮件。这段代码出了什么问题。
  • 如果错误 1053 不再出现,请将答案标记为接受。然后问一个关于邮件问题的新问题,以便更明显;这样你会得到更多的答案。寻找类似问题的用户将能够找到潜在的答案 :) 每个人都会从中受益。
猜你喜欢
  • 2011-03-28
  • 2014-08-05
  • 2011-01-05
  • 2011-12-29
  • 2015-12-28
  • 2023-03-23
  • 1970-01-01
  • 2013-12-20
相关资源
最近更新 更多