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

Add IServiceCollection extension methods to enable Steeltoe #1123

Closed
eerhardt opened this issue Apr 14, 2023 · 3 comments · Fixed by #1317
Closed

Add IServiceCollection extension methods to enable Steeltoe #1123

eerhardt opened this issue Apr 14, 2023 · 3 comments · Fixed by #1317
Labels
ReleaseLine/4.x Identified as a feature/fix for the 4.x release line Type/enhancement New feature or request
Milestone

Comments

@eerhardt
Copy link
Contributor

Is your feature request related to a problem? Please describe.

With dotnet/runtime#61634 (comment) and dotnet/runtime#65109, Microsoft.Extensions.Hosting added a new way of creating a hosted app that doesn't use IHostBuilder.

Along with that, in .NET 8, the "Worker" project template is getting updated to use these new APIs. See dotnet/aspnetcore#43113.

Describe the solution you'd like

We should add overloads to HostBuilderExtensions that accept an IServiceCollection or HostApplicationBuilder instead of an IHostBuilder. This will allow consumers using the new HostApplicationBuilder to use Steeltoe easily.

public static IHostBuilder AddSteeltoe(this IHostBuilder hostBuilder, IEnumerable<string> exclusions = null, ILoggerFactory loggerFactory = null)

Additional context

See also dotnet/runtime#68580

@TimHess
Copy link
Member

TimHess commented Apr 19, 2023

Hi @eerhardt,

Thanks for the note, I really appreciate the proactivity here.

We do already have many extensions for IServiceCollection throughout Steeltoe, and aside from the one you've pointed out I think we're already in decent shape. We were planning to review all of these for our next major release anyway though, so I'll leave this issue open as we reconsider all instances of this type of extension.

@TimHess TimHess added the ReleaseLine/4.x Identified as a feature/fix for the 4.x release line label Apr 19, 2023
@eerhardt
Copy link
Contributor Author

Thanks, @TimHess. Also check out a proposal I just opened to add an abstraction for these new application builders in .NET 8:

dotnet/runtime#85486

Looking at the above extension method, it would probably need an abstraction that is being proposed. The extension method is using a lot of things on IHostBuilder:

  • Properties
  • Configuration
  • Services

@bart-vmware
Copy link
Member

@eerhardt We've completed this work in #1317. Our chosen strategy is detailed below.

  1. We've removed all redundant host builder extension methods.

    • No host builder extension methods are provided, if we only need IConfigurationBuilder, or only IConfiguration, or only IServiceCollection, or only ILoggingBuilder, etc.
    • No host builder extension methods are provided if we additionally need IConfiguration. It can be obtained from the context parameter. For example: webHostBuilder.ConfigureServices((context, services) => { var configuration = context.Configuration; });
  2. In the cases where we need a combination (such as IConfigurationBuilder and IServiceCollection) for a feature to work properly, we offer host builder extension methods (on IHostBuilder, IWebHostBuilder, and IHostApplicationBuilder) for convenience. They call into our existing public extension methods on IConfigurationBuilder, IServiceCollection, etc.

  3. The extension methods we do offer on the various host builders are unified internally through HostBuilderWrapper, which forwards callbacks to the underlying host builder-specific methods, such as ConfigurationAppConfiguration/ConfigureLogging, etc. This enables us to keep implementation minimal.

Here's an example. We offer extension methods on the various host builders, which all send their logic through the wrapper to call existing Steeltoe extension methods on IServiceCollection and others.

public static IHostBuilder AddEnvironmentActuator(this IHostBuilder builder)
{
    HostBuilderWrapper wrapper = HostBuilderWrapper.Wrap(builder);
    InternalAddEnvironmentActuator(wrapper);
    return builder;
}

public static IWebHostBuilder AddEnvironmentActuator(this IWebHostBuilder builder)
{
    HostBuilderWrapper wrapper = HostBuilderWrapper.Wrap(builder);
    InternalAddEnvironmentActuator(wrapper);
    return builder;
}

public static IHostApplicationBuilder AddEnvironmentActuator(this IHostApplicationBuilder builder)
{
    HostBuilderWrapper wrapper = HostBuilderWrapper.Wrap(builder);
    InternalAddEnvironmentActuator(wrapper);
    return builder;
}

private static void InternalAddEnvironmentActuator(HostBuilderWrapper wrapper)
{
    wrapper.ConfigureServices(services => services.AddEnvironmentActuator());
    wrapper.ConfigureServices(services => services.ActivateActuatorEndpoints());
    wrapper.ConfigureWebHost(webHostBuilder => webHostBuilder.AddManagementPort());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ReleaseLine/4.x Identified as a feature/fix for the 4.x release line Type/enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants