Job Scheduling with Coravel

Apr 23 2025

πŸŽ‰ Random 50% OFF – Today Only!

 

Over 700 developers already upgraded their .NET skills with Design Patterns that Deliver

 

Now it's your turn.

 

🧠 What you get:

 

πŸ“˜ Ebook 1: 5 essential design patterns (Builder, Decorator, Strategy, Adapter, Mediator) across 120 pages
πŸ“— Ebook 2: 100+ design pattern interview Q&As
πŸ’‘ Advanced techniques, real-world case studies, and a full GitHub repo with 20+ C# mini-projects

 

This blog is free β€” and so is the discount for you.

 

Use code RANDOM at checkout.

 

Join now to lock in early access when it drops - plus get everything else already inside the group.

 

Get it now

 
 

Background

 
 

Coravel is a lightweight open-source library that adds background job scheduling, queuing, caching, mailing, and event broadcasting to your .NET apps - all without requiring a separate infrastructure.

 

If you've ever wanted to schedule background jobs, queue tasks, send emails, or cache data in your .NET applications without the complexity of setting up external services like Hangfire, Redis, or Quartz.NET - Coravel is your answer.

 

Inspired by Laravel (PHP), Coravel brings elegant syntax and powerful background features to .NET with zero configuration.
It's the perfect tool for developers who want simplicity without sacrificing power.

 
 

Key Benefits:

 
 

β€’ No database or message broker required
β€’ Perfect for small to medium apps
β€’ Fully integrates with ASP.NET Core dependency injection
β€’ Clean and readable syntax

 

Let's see what you can do with Coravel.

 

 
 

Task Scheduling

 
 

You can run jobs like cleaning logs, syncing files, or sending emails on a schedule with Coravel's scheduler.

 

Create job:

public class SendDailyReportsEmailJob : IInvocable
{
    public Task Invoke()
    {
        Console.WriteLine("Sending daily report email...");
        return Task.CompletedTask;
    }
}

 

Register it:

builder.Services.AddScheduler();
builder.Services.AddTransient<SendDailyReportsEmailJob>();

 

Schedule it:

app.Services.UseScheduler(scheduler =>
{
    scheduler
        .Schedule<SendDailyReportsEmailJob>()
        .DailyAtHour(6); // Every day at 6 AM
});

 

You can also use:

 

β€’ .Hourly()
β€’ .EveryMinute()
β€’ .Weekly()
β€’ .Cron("/5 *") for advanced control

 
 

Queued Background Jobs

 
 

You can avoid blocking the main thread for tasks like sending emails or processing files.

 

Create an Invocable Job:

public class ProcessWebhook : IInvocable
{
    public Task Invoke()
    {
        Console.WriteLine("Webhook processed in background.");
        return Task.CompletedTask;
    }
}

 

Queue it:

var dispatcher = app.Services.GetService<IDispatcher>();
await dispatcher.EnqueueAsync<ProcessWebhook>();

 

In-memory only: jobs are lost if the app restarts. Ideal for non-critical tasks.

 
 

Event Broadcasting

 
 

Coravel's event system enables decoupled communication between services in the same app.

 

Define an Event:

public record OrderPlacedEvent(int OrderId);

 

Create a Listener:

public class SendThankYouEmail : IListener<OrderPlacedEvent>
{
    public Task HandleAsync(OrderPlacedEvent e)
    {
        Console.WriteLine($"Thank you email sent for order {e.OrderId}.");
        return Task.CompletedTask;
    }
}

 

Use it:

await eventDispatcher.BroadcastAsync(new OrderPlacedEvent(123));

 
 

Mailing with Razor Templates

 
 

Send beautiful, templated emails using Razor views.

 

Define a Mailable:

public class WelcomeMailable : Mailable<User>
{
    public override void Build()
    {
        To(Model.Email)
            .Subject("Welcome to our platform!")
            .View("Emails.Welcome", Model);
    }
}

 

Send it:

await mailer.SendAsync(new WelcomeMailable(user));

 

Supports view rendering from Razor Class Libraries or .cshtml files.

 
 

Mailing with Razor Templates

 
 

Send beautiful, templated emails using Razor views.

 

Define a Mailable:

public class WelcomeMailable : Mailable<User>
{
    public override void Build()
    {
        To(Model.Email)
            .Subject("Welcome to our platform!")
            .View("Emails.Welcome", Model);
    }
}

 

Send it:

await mailer.SendAsync(new WelcomeMailable(user));

 

Supports view rendering from Razor Class Libraries or .cshtml files.

 
 

Simple Caching

 
 

Built-in memory caching to store frequently used data.

 

Store and Retrieve from Cache:

var data = cache.GetOrAdd("top-products", () =>
{
    return FetchTopProducts();
}, TimeSpan.FromMinutes(15));

 

No extra configuration or Redis needed.

 
 

Real-World Example: Report Generator

 
 

Imagine an internal admin dashboard for a SaaS platform. You want to:

 

β€’ Send a daily email with usage reports to the admin
β€’ Cache report data for 30 minutes
β€’ Queue email sending in background
β€’ Use events when reports are generated

 

With Coravel:
Store and Retrieve from Cache:

public class GenerateAndSendReportJob : IInvocable
{
    private readonly ICache _cache;
    private readonly IMailer _mailer;

    public GenerateAndSendReportJob(ICache cache, IMailer mailer)
    {
        _cache = cache;
        _mailer = mailer;
    }

    public async Task Invoke()
    {
        var report = _cache.GetOrAdd("daily-report", () => GenerateReport(), TimeSpan.FromMinutes(30));
        await _mailer.SendAsync(new ReportMailable(report));
    }
}

 

Schedule it:

scheduler.Schedule<GenerateAndSendReportJob>()
    .DailyAtHour(8);

 
 

Wrapping Up

 
 

Coravel proves that powerful background tasking in .NET doesn't need to be complex.

 

It lets you focus on business logic instead of infrastructure, giving you everything you need to build responsive and maintainable apps - fast.

 

Whether you're building an admin dashboard, a SaaS backend, or a microservice with lightweight needs, Coravel is a sharp and elegant tool to keep in your arsenal.

 

You can add it through NuGet.
Here is the Coravel repo.

 

That's all from me today.

 

P.S. Follow me on YouTube.

There are 3 ways I can help you:

My Design Patterns Ebooks

1. Design Patterns that Deliver

This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.


1. Design Patterns Simplified

Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.


Join TheCodeMan.net Newsletter

Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.


Sponsorship

Promote yourself to 15,250+ subscribers by sponsoring this newsletter.



Join 15,250+ subscribers to improve your .NET Knowledge.

Powered by EmailOctopus

Subscribe to
TheCodeMan.net

Subscribe to the TheCodeMan.net and be among the 15,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.

Powered by EmailOctopus