Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: AutoActivation should support async initialization #5045

Open
alrz opened this issue Mar 14, 2024 · 2 comments
Open

[API Proposal]: AutoActivation should support async initialization #5045

alrz opened this issue Mar 14, 2024 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-fundamentals

Comments

@alrz
Copy link
Member

alrz commented Mar 14, 2024

Background and motivation

If you are using auto activation you would likely need some logic to run upon activation that could potentially require async code.

API Proposal

namespace Microsoft.Extensions.DependencyInjection;

public interface IAsyncAutoActivation : IAsyncDisposable 
{
    Task OnActivationAsync(); 
}

API Usage

class MySingleton : IAsyncAutoActivation { .. }

services.AddActivatedSingleton<MySingleton>();

Additional considerations

Async activations should not block each other, that is, they would run concurrently.

There's also the question of how to handle failed tasks which I think it should prevent the application from running.

@alrz alrz added api-suggestion Early API idea and discussion, it is NOT ready for implementation untriaged labels Mar 14, 2024
@joperezr
Copy link
Member

Thanks for the proposal @alrz.

Can you share more around an example scenario of when you think this would be useful?

Tagging @mobratil who is one of the owners of this area.

@alrz
Copy link
Member Author

alrz commented Mar 14, 2024

An example would be some broker subscription, something like:

class Subscription(IMessageBus bus) : IAsyncAutoActivation
{
  public async Task OnActivationAsync() {
    _subscription = await bus.SubscribeAsync(...);
  }
  public async ValueTask DisposeAsync() {
    await _subscription.UnsubscribeAsync()
  }
}

(there's definitely more use cases out there because I've seen requests for async init/dispose for injected services but I don't have reference at hand right now, perhaps some would also involve scoped registrations but I'm not sure this package is the right place to implement that)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-fundamentals
Projects
None yet
Development

No branches or pull requests

3 participants