Skip to content

Add TrySubscribeOnce API in IDistributedApplicationEventing #9042

Closed

Description

Updated Design for TrySubscribeOnce API

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

bool TrySubscribeOnce<T>(
    object key,
    Func<T, CancellationToken, Task> callback,
    [NotNullWhen(true)] out DistributedApplicationEventSubscription? subscription
) where T : IDistributedApplicationEvent;

// Overload: uses the IDistributedApplicationEventing instance as the key
bool TrySubscribeOnce<T>(
    Func<T, CancellationToken, Task> callback,
    [NotNullWhen(true)] out DistributedApplicationEventSubscription? subscription
) where T : IDistributedApplicationEvent;

Resource-scoped event subscription

bool TrySubscribeOnce<T>(
    IResource resource,
    object key,
    Func<T, CancellationToken, Task> callback,
    [NotNullWhen(true)] out DistributedApplicationEventSubscription? subscription
) where T : IDistributedApplicationResourceEvent;

// Overload: uses the IDistributedApplicationEventing instance as the key
bool TrySubscribeOnce<T>(
    IResource resource,
    Func<T, CancellationToken, Task> callback,
    [NotNullWhen(true)] out DistributedApplicationEventSubscription? subscription
) where T : 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.

Activity

  1. davidfowl commented on May 1, 2025

    @davidfowl
    Collaborator
  2. afscrome commented on May 1, 2025

    @afscrome
    Contributor

    Not sure I understand what this is doing from the original issue.

  3. davidfowl commented on May 1, 2025

    @davidfowl
    Collaborator

    Ability to do the equivalent of TryAddLifeCycleHook

  4. afscrome commented on May 1, 2025

    @afscrome
    Contributor

    Sweet.

  5. added this to the 9.4 milestone on May 10, 2025
  6. davidfowl commented on May 10, 2025

    @davidfowl
    Collaborator

    Mitch Denny (@mitchdenny) let's do this in 9.3 and try to remove lifecycle hook usage.

  7. mitchdenny commented on May 10, 2025

    @mitchdenny
    MemberAuthor

    Should SubscribeKeyed overwrite the callback or no-op.

  8. davidfowl commented on May 10, 2025

    @davidfowl
    Collaborator

    Noop. Maybe it should be TrySubscribe

  9. changed the title [-]Allow SubscribeKeyed(...) on eventing.[/-] [+]Add SubscribeOnce API in IDistributedApplicationEventing[/+] on May 12, 2025
  10. changed the title [-]Add SubscribeOnce API in IDistributedApplicationEventing[/-] [+]Add TrySubscribeOnce API in IDistributedApplicationEventing[/+] on May 22, 2025
  11. mitchdenny commented on May 22, 2025

    @mitchdenny
    MemberAuthor

    Noop. Maybe it should be TrySubscribe

    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.

  12. davidfowl commented on May 22, 2025

    @davidfowl
    Collaborator

    We need to replace the usages of this so we can make sure the pattern works

  13. Md1234-18 commented on May 28, 2025

    @Md1234-18

    IMEI NO: 355147556894750

  14. maddymontaquila commented on May 14, 2026

    @maddymontaquila
    Collaborator

    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.

  15. mitchdenny commented on Jun 11, 2026

    @mitchdenny
    MemberAuthor

    I don't think we'll be adding this API.

  16. locked and limited conversation to collaborators on Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions