在ASP.NET应用程序中调用窗口服务方法

时间:2018-03-02 14:45:52

标签: c# asp.net windows-services

我在asp.net应用程序中创建了一个小的Windows服务。如何从我的asp.net应用程序中不断运行窗口服务?

示例代码

public class EmailSender : ServiceBase
{
    public int AdminId = 0;
    public EmailSender(int Id)
    {
        AdminId = Id;
        this.ServiceName = "Email Service";
        this.CanStop = true;
        this.CanPauseAndContinue = true;
        this.AutoLog = true;            
    }

    protected override void OnStart(string[] args)
    {
        //this.WriteToFile("Simple Service started {0}");
        this.SendEmail();
    }

    public void SendEmail()
    {
        try
        {
            Admin admin = Admin.GetInstance;
            var response = admin.GetAdminById(AdminId);
            IEnumerable<Employee> SuspendedEmployee = Employee.GetSuspendedEmployee(AdminId);
            foreach (var item in SuspendedEmployee)
            {
                if (DateTime.Today == item.ArchieveDateTo.AddDays(-1))
                {
                    string Message = "Hello " + response.FullName + " Your Employee " + item.FullName + " who was suspended from " + item.ArchieveDateFrom + " to " + item.ArchieveDateTo + " will be expiring tomorrow<br />. Please go to your dashboard to unarchieve the employee";
                    EmailWithAttachment.SendMail(item.Email, Message, "Suspension Reminder");
                }
            }
        }
        catch (Exception ex)
        {
            ErrorLog.WriteErrorLog(ex);
        }
    }
}

我实际上打算创建一个单独的Windows服务,但我最终在同一个项目中创建它。如何让它不断运行和发送电子邮件?

0 个答案:

没有答案