Skip to content

Investigating things for easier Aspire integration #24365

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

Merged
merged 26 commits into from
Mar 26, 2025
Merged

Conversation

mattleibow
Copy link
Member

@mattleibow mattleibow commented Aug 21, 2024

Description of Change

Imagine a world where you would be able to use .NET Aspire to also get all kinds of useful data out of your .NET MAUI app. Imagine no more, with this code that is EXACTLY what you can do ✨

This PR is part of a cross-team effort to make it easier to integrate .NET Aspire into .NET MAUI apps. Once everything is in place you will be able to easily get your apps' tracing, logging and metrics in your .NET Aspire dashboard. Inspect HTTP requests? No problem. Searching through a visually appealing overview or your ILogger calls? Easy peasy. Insights into your memory usage? Consider it done.

And then we didn't even talk about maybe the best feature that this brings: service discovery. When using Dev Tunnels and .NET Aspire you will be able to reach your backend services regardless of where they live! No more complicated code constructions that get in the way of a delightful getting started experience.

When this is merged, this is only the beginning of beautiful things to come. This is not the final, complete solution yet, stay tuned for that in the .NET 10 release notes. Or, if you're brave enough, have a play with this and please let us know so we can make sure that come November you will be able to enjoy the best experience possible.

This PR adds the following:

  • MauiAppBuilder now implements IHostApplicationBuilder
    • Most of the things are not super applicable for (mobile) apps. As we find relevant use-cases we might implement more things here. For now most of the things throw a NotImplementedException.
  • A .NET MAUI specific template for the .NET Aspire ServiceDefaults

Minimum requirements:

  • Visual Studio 17.13+

(Potential) follow up actions:

Related PRs:

@mattleibow mattleibow requested a review from a team as a code owner August 21, 2024 21:19
@@ -104,6 +109,12 @@ ILoggingBuilder InitializeLogging()
}
}

IDictionary<object, object> IHostApplicationBuilder.Properties => throw new NotImplementedException();

IHostEnvironment IHostApplicationBuilder.Environment => throw new NotImplementedException();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is often loaded from an environment variable or some json configuration. We could (somehow) detect this as either Debug or Release, or set something during dotnet publish, but it is a new concept.

Gets information about the hosting environment an application is running in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is (2) that I called out in #4393. I'm not sure what to do here either. Maybe just always returning Production? Or Development when the app is build for Debug and Production otherwise?

@mattleibow mattleibow marked this pull request as draft August 22, 2024 15:36
@mattleibow mattleibow added the area-core-hosting Extensions / Hosting / AppBuilder / Startup label Sep 11, 2024
@davidfowl
Copy link
Member

Is the plan to make these properties work?

@mattleibow
Copy link
Member Author

Yes

@jfversluis jfversluis force-pushed the dev/aspire branch 2 times, most recently from 08b89e5 to 5e3d70a Compare February 14, 2025 13:45
@jfversluis jfversluis changed the base branch from net9.0 to main February 14, 2025 13:45
@@ -29,6 +32,66 @@ public static MauiAppBuilder ConfigureDispatching(this MauiAppBuilder builder)
return builder;
}

public static MauiAppBuilder ConfigureEnvironmentVariables(this MauiAppBuilder builder)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand why we need to do this. Why can't Microsoft.Extensions.Configuration.EnvironmentVariables be used instead?

Copy link
Member

@jfversluis jfversluis Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: looking into Microsoft.Extensions.Configuration.EnvironmentVariables more I guess we maybe should be able to do all of the below with the right implementation of that as well.

If we're running on iOS or Android the environment variables on those devices/Simulators/emulators are not necessarily the ones that are on the host machine.

In the case of Android we write them to a file through the Hot Reload connection and I read them directly from there as its more stable that way. For iOS we provide them through the debugger connection which works fine.

However, it doesn't end there, I want to add some magic for not using localhost since that is also something that won't work for Android.

Having all that said, I do think we might want to look into still implementing it. I remember us having some open issues to support the settings.json files etc. so it might still be beneficial.

jonpryor pushed a commit to dotnet/android that referenced this pull request Mar 20, 2025
… in "Release" (#9933)

Context: d22f04e
Context: dotnet/aspire#4684
Context: dotnet/maui#24365
Context: https://github.com/jfversluis/MauiAspire

Commit d22f04e set the default value of the
[`$(HttpActivityPropagationSupport)' MSBuild property][0] to false,
which "removes code related to diagnostics support for
System.Net.Http."  This was done to reduce app size.

.NET Aspire also uses System.Net.Http, and we'd like to System.Net.Http
diagnostics support to be available by default in that environment;
see also dotnet/aspire#4684 and dotnet/maui#24365.

Update the `$(HttpActivityPropagationSupport)` MSBuild property to be
false by default only in "Release" configuration builds, i.e.
when `$(Optimize)`=true.

[0]: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options
jonpryor pushed a commit to dotnet/android that referenced this pull request Mar 20, 2025
…9928)

Context: eb6397d
Context: 200f843
Context: dotnet/runtime#89880
Context: dotnet/aspire#4684
Context: dotnet/maui#24365
Context: https://github.com/jfversluis/MauiAspire

.NET 8 introduced use of System.Diagnostics.Metrics into `HttpClient`,
which immediately slowed down .NET for Android app startup times
(dotnet/runtime#89880).  This was "fixed" by *disabling* use of Metrics
within .NET for Android apps *by default* (eb6397d, 200f843).
Support for Metrics could be re-enabled by setting the
[`$(MetricsSupport)` MSBuild property][0].

.NET Aspire also uses System.Diagnostics.Metrics, and we'd like to
allow Metrics to be available by default in that environment; see also
dotnet/aspire#4684 and dotnet/maui#24365.

Update the `$(MetricsSupport)` MSBuild property to be false by default
only in "Release" configuration builds,, i.e. when `$(Optimize)`=true.

[0]: https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options
@jfversluis jfversluis merged commit 23167be into net10.0 Mar 26, 2025
74 of 128 checks passed
@jfversluis jfversluis deleted the dev/aspire branch March 26, 2025 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-hosting Extensions / Hosting / AppBuilder / Startup t/breaking 💥
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants