简体   繁体   中英

Scheduling events in MVC / IIS (DDD)

My domain contains an event system that is expected to send out emails if a certain date/time is reached, so I'll have to continously check registered events for their 'invoke date'; which is, to my understanding, a concern of an application service.

However, the application will be hosted as a MVC application on IIS which doesn't support stateful components (except the 'session'), so I'm wondering what would be a proper solution for this case.

Scheduling tasks in an ASP.NET MVC project is possible using the Revalee open source project.

Revalee is a service that allows you to schedule web callbacks to your web application). In your case, you would schedule a callback that would send out emails at a specific time. Revalee works very well with tasks that are discrete transactional actions, like sending an automated email message or updating a database value (read: not long running). The code to perform your action would all reside within your MVC app.

Let's assume for the example below that you need to send an email message (something like, "your trial plan is about expire") to a new user 27 days after they registered on your website.

To use Revalee, you would:

  1. Install the Revalee Service, a Windows Service, on your server. The Windows Service is available in the source code (which you would compile yourself) or in a precompiled version available at the Revalee website .

  2. Use the MVC-specific Revalee client library in your Visual Studio project. (There is a non-MVC version too.) The client library is available in the source code (which, again, you would compile yourself) or in a precompiled version available via NuGet .

  3. You would register a future callback when your new website user registers ScheduleExpirationMessage() method (this example is assuming that you need your action to 27 days from now).

     private void ScheduleExpirationMessage(int userId) { DateTimeOffset callbackTime = DateTimeOffset.Now.AddDays(27.0); // The callback should be in 27 days from now Uri callbackUrl = new Uri(string.Format("http://mywebapp.com/ScheduledCallback/SendExpirationMessage/{0}", userId)); // Register the callback request with the Revalee service RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl); } 
  4. When Revalee calls your application back, your app would perform whatever action you have coded it to do. In the ScheduledCallback controller, your SendExpirationMessage action (see the Uri above) might look like:

     [AllowAnonymous] [CallbackAction] public ActionResult SendExpirationMessage(int userId) { // TODO Lookup the user's information and send the trial expiration message // ... return new EmptyResult(); } 

I hope this helps.

Note: The code example above uses a synchronous version of ScheduleCallback() , the Revalee client library also supports asynchronous calls à la:

RevaleeRegistrar.ScheduleCallbackAsync(callbackTime, callbackUrl);

I was one of the developers involved with the Revalee project. 我是参与Revalee项目的开发人员之一。 To be clear, however, Revalee is free, open source software. The source code is available on GitHub .

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