aspire icon indicating copy to clipboard operation
aspire copied to clipboard

Preserve the type of application builder in chained extension methods

Open sliekens opened this issue 1 year ago • 2 comments

This is a slight improvement to the usability of extensions in the ServiceDefaults project template. Previously, you couldn't chain WebApplication.CreateBuilder and the ServiceDefaults extensions.

For example, this code won't compile:

var builder = WebApplication.CreateBuilder(args)
    .AddServiceDefaults();

var app = builder.Build();
               // ^^^^^
               // CS1061: 'IHostApplicationBuilder' does not contain a definition for 'Build' and no accessible extension method 'Build' accepting a first argument of type 'IHostApplicationBuilder' could be found

After this change, compilation will succeed.

Microsoft Reviewers: Open in CodeFlow

sliekens avatar Jul 02 '24 21:07 sliekens

@mitchdenny @DamianEdwards @davidfowl - thoughts here?

If we take this change, we need to update ServiceDefaults everywhere - playground apps, all the templates, etc.

eerhardt avatar Jul 22 '24 17:07 eerhardt

I don't have a problem with this change, but the PR will need to include the fixes to playground apps etc as @eerhardt has identified.

I would defer to folks who have had to deal with the reality of project template changes over time and what issues that can introduce though.

mitchdenny avatar Aug 07 '24 05:08 mitchdenny

This seems like something we should consider for 9.0

DamianEdwards avatar Aug 23 '24 18:08 DamianEdwards

@sliekens did you want to add the playground app updates here too?

mitchdenny avatar Aug 26 '24 02:08 mitchdenny

Yeah I realized after creating the PR that the ServiceDefaults template was not the only place to make changes, though I'm not sure how many other projects would need to be changed.

sliekens avatar Aug 26 '24 06:08 sliekens

If you look under /playground you'll see that we generally share a single service defaults project there.

mitchdenny avatar Sep 01 '24 23:09 mitchdenny

@mitchdenny I'm noticing other kinds of drift between the different copies of the Extensions.cs, is it okay if I re-align them while I'm making this change?

.e.g. #4096 removed broken Prometheus examples from the templates but they are still present in the playground apps.

sliekens avatar Sep 02 '24 17:09 sliekens

@DamianEdwards thanks for the review, I removed the unused directives and I updated every usage I could find of IHostApplicationBuilder in the playground.

sliekens avatar Sep 24 '24 17:09 sliekens