I'm trying to create window service which sends mail to me when specific type of file came in the folder.While running the service i'm getting error like 'Error 1053- the service did not respond to the start or control request in a timely fashion. Please help me to resolve this error.' I will be more thankful for you.
I have written code as below:
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>
You should try offloading the work done by BrlCode in a different thread so that OnStart can return as fast as possible.
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();
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.