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

Improve DependencyInjection.AutoActivationExtensions #4219

Merged
merged 1 commit into from
Jul 27, 2023
Merged

Conversation

geeknoid
Copy link
Member

@geeknoid geeknoid commented Jul 27, 2023

  • Avoid calling MakeGenericType when possible.

  • Delete the now-useless Hosting.StartupINitialization component.

Microsoft Reviewers: Open in CodeFlow

@ghost ghost assigned geeknoid Jul 27, 2023
@martintmk
Copy link
Contributor

@geeknoid, just wondering.

What is the replacement for IStartupInitialization?

@geeknoid
Copy link
Member Author

@martintmk @ericstj What's the actual initialization API that was added to dotnet/runtime?

@geeknoid
Copy link
Member Author

Ah, I think this is it: dotnet/runtime#87335

@geeknoid
Copy link
Member Author

@steveharter Note that I'll add support for keyed services in a follow-on PR. I've got it all coded, but I need to wait for dotnet/runtime#89447 to be fixed, otherwise none of it works.

- Avoid calling MakeGenericType when possible.

- Delete the now-useless Hosting.StartupINitialization component.
@ericstj
Copy link
Member

ericstj commented Jul 27, 2023

Ah, I think this is it: dotnet/runtime#87335

Yes - the decision was made to not create a new initializer concept but instead incorporate the ask into hosted services themselves.

@rafal-mz rafal-mz merged commit 1c4812d into main Jul 27, 2023
6 checks passed
@rafal-mz rafal-mz deleted the geeknoid/autoact branch July 27, 2023 19:10
@ghost ghost added this to the 8.0 RC1 milestone Jul 27, 2023
RussKie added a commit to RussKie/dotnet-extensions-experimental that referenced this pull request Aug 3, 2023
@ericstj
Copy link
Member

ericstj commented Aug 3, 2023

@steveharter can help with these - I believe you just need an IHostedLifecycleService implementation instead of delegate. You might even be able to use an existing IHostedService in the same library to do the job if one exists.

@steveharter
Copy link
Member

Yes IHostedLifecycleService.Starting should be used since that occurs before IHostedService.Start.

Here's a pattern from an earlier prototype of mine that has a fallback in case the host doesn't support the new interface:

  internal sealed class StartupActivator : IHostedLifecycleService
  {
      private bool _done;

      Task IHostedLifecycleService.StartingAsync(CancellationToken cancellationToken) => Activate();

      // For backwards compat with hosts that don't support IHostedLifecycleService, use StartAsync().
      Task IHostedService.StartAsync(CancellationToken cancellationToken) => Activate();

      Task IHostedLifecycleService.StartedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
      Task IHostedService.StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
      Task IHostedLifecycleService.StoppedAsync(CancellationToken cancellationToken) => Task.CompletedTask;
      Task IHostedLifecycleService.StoppingAsync(CancellationToken cancellationToken) => Task.CompletedTask;

      private Task Activate()
      {
          if (!_done)
          {
              // activate the service(s) here
              _done = true;
          }

          return Task.CompletedTask;
      }
}

@ghost ghost locked as resolved and limited conversation to collaborators Sep 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants