简体   繁体   中英

how to make scheduled process in asp.net/C# with mvc5

I have a MVC5 application that has a function to send e-mail to user after subscribing the newsletter. Now i have a requirement to send e-mail to all those users whose are running 1 month ahead of expiry date of their subscription. In this case i need to implement a background process that will run every day at a specific time on the web server. How can i do that?

Thanks

Personally, I'd create a separate dedicated Windows Service to periodically check for expiry, and then call the code to send the emails.

An alternative is to create a simple console application to run the task, and call it using the Windows Task Scheduler.

A less secure method is to use a ping service, to periodically hit a page with an obfuscated URL which processes the emails.

A slightly old, but relevant blog post, detailing the issues with recurrent background tasks in .net sites can be read here .

You can create a Windows Service to make that work for you. You should follow this tutorial . You can store the date/time on the project web.config/app.config which when you want your service to be executed. When the service executes, you validate the time and call a generic function that will do what you want. Follow this example:

public partial class Service1 : ServiceBase
{
    YourServiceClass service;
    private Timer serviceTimer;

    public Service1()
    {
        InitializeComponent();
        service = new YourServiceClass();
    }

    protected override void OnStart(string[] args)
    {
        TimerCallback timerDelegate = new TimerCallback(service.GetData); // You should add this function to your class. You have an example below

        string time = System.Configuration.ConfigurationSettings.AppSettings["SceduleTime"];    // Gets time from app.config like 12:50
        string[] timeS = time.Split(':');

        DateTime DateIni = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, Convert.ToInt32(timeS[0]), Convert.ToInt32(timeS[1]), 0);

        TimeSpan diff = DateTime.Now - DateIni;
        if (diff.TotalSeconds < 0)
            diff = DateIni - DateTime.Now;
        else
            diff = DateIni.AddDays(1) - DateTime.Now;

        // create timer and attach our method delegate to it
        serviceTimer = new Timer(timerDelegate, service, diff, new TimeSpan(1, 0, 0, 0));
    }

    protected override void OnStop()
    {
        serviceTimer.Dispose();
    }
}

And on your YourServiceClass class, you add this function:

public void GetData(object state){
    // Do something...
}

Hope it helps!

It really depends on your requirements, and whether you want this update to be truly independent on the server, in a web application or bundled with your app.

I recently found a great third party library for ASP.NET called Hang Fire .

It looks like it can be as simple as:

RecurringJob.AddOrUpdate(
    () => Console.WriteLine("Transparent!"), 
    Cron.Daily);

It supports:

  • Delayed Jobs
  • Recurring Tasks
  • Automatic Retries
  • And it has a nicely themed Management/Reporting interface

I'm planning to use it for some scheduled batch tasks I need to run on a CRM system. No experience with it yet, but it does seem to have some great features and I think I've seen it recommended a few times around Stack Overflow!

There are a ton of different ways to approach what you want (Scheduled Task on the server is another off the top of my head). This is a nice little package that lets me bundle it with my web applications.

I recommend you to use Quartz library. Your process should be independent from your web application since the web app will recycle from time to time based on the user traffic which will trigger Application_End event and will start once a new request is sent which will trigger Application_Start so you wont be able to manage correctly your schedule start and end.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM