Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Created May 8, 2025 07:59
Show Gist options
  • Save ramonsmits/0ccb62a2f67491d00638ee206048b28a to your computer and use it in GitHub Desktop.
Save ramonsmits/0ccb62a2f67491d00638ee206048b28a to your computer and use it in GitHub Desktop.
NServiceBus - Unsubscribe from events during startup
using System;
using Microsoft.Extensions.Hosting;
class UnsubscribeHostedService(IMessageSession messageSession : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await messageSession.Unsubscribe<MyEvent>();
}
}
// var host = Host.CreateDefaultBuilder(args)
// .ConfigureServices(services => {
// services.AddHostedService<UnsubscribeHostedService>();
// })
// .Build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment