# .net core任务调度_探索.NET Core库Coravel以进行任务调度，缓存，邮件发送等

.net core任务调度

Coravel claims it is a "Near-zero config .NET Core library that makes Task Scheduling, Caching, Queuing, Mailing, Event Broadcasting (and more) a breeze!" A lovely claim, that is, in fact, true! It's open source and on Github at https://github.com/jamesmh/coravel so give Coravel a star!

Coravel声称这是一个“近零配置.NET Core库，使任务调度，缓存，排队，邮件，事件广播(以及更多)变得轻而易举！” 一个不错的主张，就是事实，是真的！ 它是开源的，可以在Github上的https://github.com/jamesmh/coravel上找到，所以给Coravel一颗星星吧！

Coravel is available on NuGet as a package - as are all things  - or you can also install it's helper CLI with a simple dotnet tool install --global coravel-cli. After this, using coravel easy, early, and often is as simple as:

coravel install


A nice teach that makes it easy, the coravel CLI adds the package reference, restores your project, and reminds you to set it up in ConfigureServices() in Startup.cs. A nice example of a thoughtful library that is trying to make onboarding simpler.

coravel CLI很好地教了它，使它变得容易，它添加了程序包引用，还原了项目，并提醒您在Startup.cs的ConfigureServices()中进行设置。 一个深思熟虑的库的一个很好的示例，它试图简化入门过程。

The Coravel CLI is also a nice scaffolder to get you started with item templates:

Coravel CLI还是一个不错的脚手架，可让您开始使用项目模板：

> coravelUsage: coravel [options] [command]Options:  -?|-h|--help  Show help informationCommands:  event  install  invocable  mail


### 那是什么 (But what is it?)

With a somewhat vague name and a list of cool features that may not seem related, you may find yourself wondering WHAT is this and WHY do I need it?

When you start thinking about layering and responsibilities of real production software, you'll note that there are arguably some gaps in the BCL (Base Class Libraries) that .NET makes available, particularly as you move up into the Application Development space.

Scheduled jobs and tasks, simple emailing with Razor Templates, a lightweight event dispatcher, easily queueable background tasks are just some of the higher level primitives you'll find yourself wanting when creating business apps. Coravel collects those buildable elements and allows you to string them together very quickly.

For example, I'll create an "Invocable." Basically just a method that is more 'job-like.' It has business scope and I need to have it invoked later by some schedule or process within my app.

Here I'll register one in my Startup.cs.

services.AddScoped<SendNightlyReportsEmailJob>();



So I need to send a nightly report. That's an invocable thing, and it's also an IMailer because it mails things. Note the injected IMailer in the constructor. All very natural in ASP.NET Core, using Dependency Injection.

public class SendNightlyReportsEmailJob : IInvocable{    private IMailer _mailer;    public SendNightlyReportsEmailJob(IMailer mailer)    {        this._mailer = mailer;    }    public async Task Invoke()    {        Console.WriteLine("NightlyReportMailable Started....");        await Task.Delay(10000);        // You could grab multiple users from a DB query ;)        var mailable = new NightlyReportMailable(new UserModel        {            Name = "Coravel is lovely!",            Email = "test@test.com"        });        await this._mailer.SendAsync(mailable);        Console.WriteLine(\$"NightlyReportMailable was sent at {DateTime.UtcNow}.");    }}


Then I can have this mailed every evening with the Coravel Scheduler:

scheduler.Schedule<SendNightlyReportsEmailJob>().Daily();



But when, right? Easy:

scheduler    .Schedule<SendNightlyReportsEmailJob>()    .DailyAt(1, 30)    .Zoned(TimeZoneInfo.Local);



What if you have a task that needs to happen, but maybe it's either long-running or happening often. You don't want two tasks going at the same time, so PreventOverlapping! Clever.

scheduler    .Schedule<DoAThingOften>()    .EveryMinute()    .PreventOverlapping("DoAThingOften");



And that's just the scheduler. That mail you have to send? You can use Razor Pages to create reach HTML emails! That makes New User Sign Up, or Completed Order very easy to create. All self-contained in your app. I dig it.

Finally note that there's Pro paid version of Coravel that gives you a very professional UI for Jobs and Invocables, and allows you to visually configure your back-end job schedules. A nice way to support open source - especially when you start using it and really depending on it - is to explore very reasonable Pro licenses like those that Pro Coravel has. Impressive stuff, truly.

*I have no relationship with Coravel the project or the Pro licenses, I'm just a fan.

*我与Coravel项目或Pro许可证没有关系，我只是一个粉丝。

.net core任务调度

06-19 141
12-05 391
10-12 394
07-10 2159
03-18 507
08-28 132
04-18 209

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

©️2022 CSDN 皮肤主题：大白 设计师：CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。