Coravel is an open source tool library in. NET Core that makes it easy for you to use advanced applications such as timed tasks, caching, queuing, events, and broadcasts!

Coravel is an open source tool library in. NET Core that makes it easy for you to use advanced applications such as timed tasks, caching, queuing, events, and broadcasts!

Coravel helps developers quickly get up and running their. NET Core applications without compromising code quality.

最后更新 7/7/2022 7:34 AM
黑哥聊dotNet
预计阅读 4 分钟
分类
.NET
标签
.NET C# open source

Coravel

Coravel is an open source tool library in. NET Core that makes it easy for you to use advanced applications such as timed tasks, caching, queuing, events, and broadcasts!

Coravel helps developers quickly get up and running their. NET Core applications without compromising code quality.

It makes advanced application features easy to access and use by providing you with simple, expressive and straightforward syntax.

GitHub 地址: https://github.com/jamesmh/coravel

installation

dotnet add package coravel

example

Task Scheduling

    • Configuration **

In the Startup.cs file of the. NET Core application, within the ConfigureServices () method, add the following:

services.AddScheduler()
    • Use **

Then in the Configure () method, you can use the scheduler:

var provider = app.ApplicationServices;
provider.UseScheduler(scheduler =>
{
    scheduler.Schedule(
        () => Console.WriteLine("Every minute during the week.")
    )
    .EveryMinute()
    .Weekday();
});

Queuing

    • Configuration **

In your Startup file, in ConfigureServices ():

services.AddQueue();
    • Use **

将接口的一个实例Coravel.Queuing.Interfaces.IQueue注入到控制器

IQueue _queue;

public HomeController(IQueue queue) {
    this._queue = queue;
}
    • Sync **
public IActionResult QueueTask() {
    this._queue.QueueTask(() => Console.WriteLine("This was queued!"));
    return Ok();
}
    • Asynchronous **
 this._queue.QueueAsyncTask(async() => {
    await Task.Delay(1000);
    Console.WriteLine("This was queued!");
 });

Caching

    • Configuration **

In Startup. ConfigureServices ():

services.AddCache();

This will enable memory (RAM) caching.

    • Use **

要使用缓存,将Coravel.Cache.Interfaces.ICache通过依赖注入进行注入。

private ICache _cache;

public CacheController(ICache cache)
{
    this._cache = cache;
}

Event Broadcasting

Coravel's event broadcasts allow listeners to subscribe to events that occur in the application.

    • Configuration **

In the ConfigureServices method:

services.AddEvents();

Next, in the Configure method:

var provider = app.ApplicationServices;
IEventRegistration registration = provider.ConfigureEvents();

Register events and their listeners

registration
 .Register<BlogPostCreated>()
 .Subscribe<TweetNewPost>()
   .Subscribe<NotifyEmailSubscribersOfNewPost>();
    • Use **

Create a class Coravel.Events. Interfaces.IEvent. That's it!

Events are just data objects that will be provided to each listener. It should expose the data associated with this specific event.

For example, a BlogPostCreated event should accept what BlogPost created and then expose it through public attributes.

public class BlogPostCreated : IEvent
{
    public BlogPost Post { get; set; }

    public BlogPostCreated(BlogPost post)
    {
        this.Post = post;
    }
}

创建一个新类,该类实现您将要监听的事件Coravel.Events.Interfaces.IListener的接口。提示:每个侦听器只能与一个事件相关联。

该 IListener 接口需要您实现HandleAsync(TEvent broadcasted)

Create a listener called TweetNewPost:

public class TweetNewPost : IListener<BlogPostCreated>
{
    private TweetingService _tweeter;

    public TweetNewPost(TweetingService tweeter){
        this._tweeter = tweeter;
    }

    public async Task HandleAsync(BlogPostCreated broadcasted)
    {
        var post = broadcasted.Post;
        await this._tweeter.TweetNewPost(post);
    }
}

Mailing

    • Configuration **

安装 NuGet 包Coravel.Mailer,搭建一些基本文件:

  • ~/Views/Mail/_ViewStart. cshtml-Configure the mail view to use Coravel's email template
  • ~/Views/Mail/_ViewImports. cshtml-allows you to use Coravel's view component
  • ~/Views/Mail/Example. cshtml-Example Mail View
  • ~/Mailables/Example. cs-mailable samples

In Startup. ConfigureServices ():

services.AddMailer(this.Configuration);
    • Use **

Coravel 使用Mailables发送邮件。Mailables 继承Coravel.Mailer.Mail.Mailable并接受一个泛型类型,该类型表示您希望与发送邮件相关联的模型。

using Coravel.Mailer.Mail;
using App.Models;

namespace App.Mailables
{
  public class NewUserViewMailable : Mailable<UserModel>
  {
      private UserModel _user;

      public NewUserViewMailable(UserModel user) => this._user = user;

      public override void Build()
      {
          this.To(this._user)
              .From("from@test.com")
              .View("~/Views/Mail/NewUser.cshtml", this._user);
      }
  }
}

All configuration of Mailable is completed in this Build () method. You can then call various methods, such as To and From, to configure recipients, senders, and so on.

If you are interested in.net open source projects, you can continue to pay attention to me.

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读
同分类 / 同标签 2/7/2026

Summary of experience in using AOT

From the very beginning of project creation, you should develop a good habit of conducting AOT release testing in a timely manner whenever new features are added or newer syntax is used.

继续阅读