Skip to content

Commit 03519b3

Browse files
ramonsmitslailabougriaafprtclr
authored
UnsubscribeBackgroundService (#7119)
Co-authored-by: Laila Bougria <[email protected]> Co-authored-by: Adam Furmanek <[email protected]>
1 parent bdb9ff8 commit 03519b3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

nservicebus/messaging/publish-subscribe/controlling-what-is-subscribed.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,19 @@ Events can manually be subscribed and unsubscribed to:
5656
snippet: ExplicitSubscribe
5757

5858
In NServiceBus version 6 and above, `Subscribe` and `Unsubscribe` are accessible via the `IMessageSession` available on the `IEndpointInstance` or within a [feature startup task](/nservicebus/pipeline/features.md#feature-startup-tasks).
59+
60+
## Decomissioning event handlers
61+
62+
An event is not automatically unsubscribed when a message handler is removed. The subscription remains active until it is unsubscribed.
63+
64+
Unsubscribing can be done at startup via `IMessageSession.Unsubscribe`. For example, via a background service:
65+
66+
```c#
67+
public sealed class UnsubscribeService(IMessageSession messageSession) : BackgroundService
68+
{
69+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
70+
{
71+
await messageSession.Unsubscribe<MyEvent>();
72+
}
73+
}
74+
```

0 commit comments

Comments
 (0)