Window Service
Installing
dotnet add package Microsoft.Extensions.Hosting.WindowsServices
Configuration
builder.Services.AddWindowsService(options =>
{
options.ServiceName = "AutoSendEmail";
});
builder.Services.AddHostedService<Worker>();
Create Class Contact
public class Contact{
public int ContactId { get; set; }
public string Email { get; set; } = null!;
public string Subject { get; set; } = null!;
public string Body { get; set; } = null!;
}
Create Class BaseRepository
public abstract class BaseRepository{
protected IConfiguration configuration;
public BaseRepository(IConfiguration configuration) => this.configuration = configuration;
}
Create Class ContactRepository
public class ContactRepository : BaseRepository{
public ContactRepository(IConfiguration configuration) : base(configuration){ }
public IEnumerable GetContacts(){
using(IDbConnection connection = new SqlConnection(configuration.GetConnectionString("Store"))){
return connection.Query("SELECT * FROM Contact WHERE EndTime IS NULL AND StartTime <= GETDATE();");
}
}
public int Update(IEnumerable list){
using(IDbConnection connection = new SqlConnection(configuration.GetConnectionString("Store"))){
string sql = "UPDATE Contact SET EndTime = GETDATE() WHERE ContactId IN @Ids";
return connection.Execute(sql, new { Ids = list});
}
}
}
Class MailSettings
public class MailSettings{
public string Host { get; set; } = null!;
public short Port { get; set; }
public string Email { get; set; } = null!;
public string Password { get; set; } = null!;
}
Class EmailSender
public class EmailSender
{
MailSettings settings;
public EmailSender(IOptions options) => settings = options.Value;
public async Task Send(Contact obj){
try{
using SmtpClient client = new SmtpClient{
Host = settings.Host,
EnableSsl = true,
Port = settings.Port,
Credentials = new NetworkCredential(settings.Email, settings.Password),
UseDefaultCredentials = false
};
var message = new MailMessage{
From = new MailAddress(settings.Email),
Subject = obj.Subject,
Body = obj.Body
};
message.To.Add(new MailAddress(obj.Email));
await client.SendMailAsync(message);
return 1;
}catch{
return 0;
}
}
}
Class Worker
public class Worker : BackgroundService{
EmailSender sender;
ContactRepository repository;
public Worker(EmailSender sender, IConfiguration configuration) {
this.sender = sender;
repository = new ContactRepository(configuration);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
IEnumerable contacts = repository.GetContacts();
if(contacts != null && contacts.Count() > 0){
List list = new List();
foreach (Contact item in contacts){
if(await sender.Send(item) > 0){
list.Add(item.ContactId);
}
}
repository.Update(list);
}
await Task.Delay(10000, stoppingToken);
}
}
}
Publish Service
dotnet publish --output "D:\Services"
Create Service
sc.exe create "AutoSendEmail" binpath="d:\Services\AutoSendEmail.exe"
Start Service
sc.exe start "AutoSendEmail"
Stop Service
sc.exe stop "AutoSendEmail"
Delete Service
sc.exe delete "AutoSendEmail"