You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add TrySubscribeOnce API in IDistributedApplicationEventing #9042
This issue is to introduce idempotent subscription support to eventing, similar to TryAddLifecycleHook<T>, but for distributed application events.
Proposed API: TrySubscribeOnce
This API will provide both resource and non-resource variants, mirroring the existing Subscribe methods. The callback uses Func<T, CancellationToken, Task>, and the API is generic for event type consistency.
Non-resource event subscription
boolTrySubscribeOnce<T>(objectkey,Func<T,CancellationToken,Task>callback,[NotNullWhen(true)]outDistributedApplicationEventSubscription?subscription)whereT:IDistributedApplicationEvent;// Overload: uses the IDistributedApplicationEventing instance as the keyboolTrySubscribeOnce<T>(Func<T,CancellationToken,Task>callback,[NotNullWhen(true)]outDistributedApplicationEventSubscription?subscription)whereT:IDistributedApplicationEvent;
Resource-scoped event subscription
boolTrySubscribeOnce<T>(IResourceresource,objectkey,Func<T,CancellationToken,Task>callback,[NotNullWhen(true)]outDistributedApplicationEventSubscription?subscription)whereT:IDistributedApplicationResourceEvent;// Overload: uses the IDistributedApplicationEventing instance as the keyboolTrySubscribeOnce<T>(IResourceresource,Func<T,CancellationToken,Task>callback,[NotNullWhen(true)]outDistributedApplicationEventSubscription?subscription)whereT:IDistributedApplicationResourceEvent;
Behavior
The key can be any object. This enables idempotency per logical subscription and supports both arbitrary keys and the "type as key" lifecycle hook pattern.
If a subscription with the key already exists, the call is a no-op, returns false, and the out var is null.
If a subscription is added, the call returns true and the out var contains the subscription.
The [NotNullWhen(true)] attribute is used on the out parameter for better nullability analysis.
Reusing a key is expected and safe; this is the purpose of the API.
No migration or removal of lifecycle hook usage will be performed in this PR.
Rationale
Mirrors the existing Subscribe API (generic, resource/non-resource, async callback).
Makes it easy to enforce "subscribe once" semantics.
Flexible: supports both "type as key" and arbitrary key scenarios.
This issue description was updated to include the detailed API design and the use of [NotNullWhen(true)] for the out subscription parameter.
changed the title [-]Add SubscribeOnce API in IDistributedApplicationEventing[/-][+]Add TrySubscribeOnce API in IDistributedApplicationEventing[/+]on May 22, 2025
I opted for TrySubscribeOnce to make it crystal clear. I also told copilot to implement an overload that doesn't take a key, in that case it'll use the IDistributedApplicationEventing as the key and this will mostly mimic the API behavior. Once we've got the first cut of this is merged we should be able to go through and replace all usages of lifecycle hooks and add the obsolete attrbiute.
Activity
davidfowl commentedon May 1, 2025
cc Alex Crome (@afscrome)
afscrome commentedon May 1, 2025
Not sure I understand what this is doing from the original issue.
davidfowl commentedon May 1, 2025
Ability to do the equivalent of TryAddLifeCycleHook
afscrome commentedon May 1, 2025
Sweet.
davidfowl commentedon May 10, 2025
Mitch Denny (@mitchdenny) let's do this in 9.3 and try to remove lifecycle hook usage.
mitchdenny commentedon May 10, 2025
Should SubscribeKeyed overwrite the callback or no-op.
davidfowl commentedon May 10, 2025
Noop. Maybe it should be TrySubscribe
[-]Allow SubscribeKeyed(...) on eventing.[/-][+]Add SubscribeOnce API in IDistributedApplicationEventing[/+][-]Add SubscribeOnce API in IDistributedApplicationEventing[/-][+]Add TrySubscribeOnce API in IDistributedApplicationEventing[/+]mitchdenny commentedon May 22, 2025
I opted for TrySubscribeOnce to make it crystal clear. I also told copilot to implement an overload that doesn't take a key, in that case it'll use the IDistributedApplicationEventing as the key and this will mostly mimic the API behavior. Once we've got the first cut of this is merged we should be able to go through and replace all usages of lifecycle hooks and add the obsolete attrbiute.
davidfowl commentedon May 22, 2025
We need to replace the usages of this so we can make sure the pattern works
Md1234-18 commentedon May 28, 2025
IMEI NO: 355147556894750
maddymontaquila commentedon May 14, 2026
This has been implemented in #11266. Closing as resolved. Thank you for the proposal! If you have follow-up feature requests, please open a new issue.
mitchdenny commentedon Jun 11, 2026
I don't think we'll be adding this API.