Desktop, web and worker apps
PulseSharp delivers events and runs jobs in a hosted service, so it works in any app built on the .NET Generic Host. It has no dependency on ASP.NET Core.
Web app (ASP.NET Core)
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddPulseSharp(pulse =>
{
pulse.UsePostgreSql(builder.Configuration.GetConnectionString("Events")!);
pulse.AddHandler<SaleCompleted, PrintReceipt>();
});
var app = builder.Build();
app.MapPost("/sales", async (Sale sale, IPulse pulse) =>
{
await pulse.Emit(new SaleStream(sale.Id), new SaleCompleted(sale.Id, sale.Total));
return Results.Accepted();
});
app.Run();Worker service
dotnet new worker -n Workersvar builder = Host.CreateApplicationBuilder(args);
builder.Services.AddPulseSharp(pulse =>
{
pulse.UsePostgreSql(builder.Configuration.GetConnectionString("Events")!);
pulse.AddJobHandler<ExportReport, ExportReportHandler>();
});
builder.Build().Run();Desktop app (WPF, WinForms, Avalonia, MAUI)
Build a host when the app starts, start it, and stop it when the app closes:
public partial class App
{
private IHost? _host;
protected override async void OnStartup(StartupEventArgs e)
{
var builder = Host.CreateApplicationBuilder();
builder.Services.AddPulseSharp(pulse =>
{
pulse.UseSqlite("till.db");
pulse.Node = "till-1";
pulse.AddHandler<SaleCompleted, PrintReceipt>();
});
builder.Services.AddSingleton<MainWindow>();
_host = builder.Build();
await _host.StartAsync();
_host.Services.GetRequiredService<MainWindow>().Show();
}
protected override async void OnExit(ExitEventArgs e)
{
await _host!.StopAsync();
_host.Dispose();
}
}Handlers run on background threads. To update the UI from a handler, marshal to the UI thread (Dispatcher.InvokeAsync in WPF, Dispatcher.UIThread.Post in Avalonia).
Offline-first desktop apps
With pulse.UseSqlite("till.db"), a desktop app keeps its events and jobs on disk and continues after a restart - see Stores. Device to cloud sync sends them to the server when the device is online. If the app is killed instead of closed, its handlers start again within 10 seconds when it restarts: see When an app stops suddenly.
Console app
See Getting started.