Skip to content

Getting started ​

In five minutes you will save events, react to them, and run a background job - in a plain console app.

1. Create the app ​

You need the .NET 8 SDK or later.

bash
dotnet new console -n PulseDemo
cd PulseDemo
dotnet add package PulseSharp.Sqlite --prerelease
dotnet add package Microsoft.Extensions.Hosting

PulseSharp.Sqlite keeps events and jobs in a small database file, so nothing is lost when the app stops. PulseSharp runs inside the .NET Generic Host, which web apps, worker services and desktop apps can all use. That is why the demo adds Microsoft.Extensions.Hosting.

2. Write the app ​

Replace Program.cs:

csharp
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using PulseSharp;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.AddPulseSharp(pulse =>
{
    pulse.UseSqlite("demo.db");

    pulse.On<SaleCompleted>("print-sales", (sale, context, ct) =>
    {
        Console.WriteLine($"Sale {sale.Number}: {sale.Total:C}");
        return Task.CompletedTask;
    });

    pulse.OnJob<ExportReport>(async (job, context, ct) =>
    {
        await Task.Delay(500, ct);
        Console.WriteLine($"Report for {job.Day:yyyy-MM-dd} exported.");
    });
});

using var host = builder.Build();
await host.StartAsync();

var pulse = host.Services.GetRequiredService<IPulse>();

await pulse.Emit(new SaleCompleted(1, 12.50m));
await pulse.Emit(new SaleCompleted(2, 4.99m));
await pulse.Enqueue(new ExportReport(DateOnly.FromDateTime(DateTime.Today)));

await Task.Delay(1500);
await host.StopAsync();

public sealed record SaleCompleted(int Number, decimal Total);

public sealed record ExportReport(DateOnly Day);

What is going on:

  • An event (SaleCompleted) is a plain record that says something happened. A job (ExportReport) is a plain record that says what work to do.
  • AddPulseSharp(…) sets everything up, and UseSqlite("demo.db") keeps the data in demo.db.
  • On<SaleCompleted>("print-sales", …) runs your code for every sale, in the order the sales happened. PulseSharp remembers how far print-sales has got under that name.
  • OnJob<ExportReport>(…) is the code that does the job.
  • ct is a cancellation token: it is cancelled when the app stops, or when a handler takes too long. Pass it on to what you await.
  • Emit saves the event and returns at once. Your handler runs in the background.
  • Enqueue saves the job. A worker runs it in the background.

3. Run it ​

bash
dotnet run
text
Sale 1: $12.50
Sale 2: $4.99
Report for 2026-10-14 exported.

Run it again. You see two sales, not four: PulseSharp remembers which events it has already handled, even after a restart.

Next steps ​

Released under the MIT License.