Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@
Condition="'$(_XFTargetsImported)' == 'true'"/>
</Target>

<Target
Name="_ValidateMauiAspireProperty"
BeforeTargets="_CheckForInvalidConfigurationAndPlatform"
Condition="'$(MauiDisableAspireValidation)' != 'True'">
<Warning
Comment thread
jfversluis marked this conversation as resolved.
Outdated
Code="MA002"
Text="The _EnableMauiAspire property should not be set manually. Using Aspire outside the Debug configuration may introduce performance and security risks in production. This property is automatically configured based on build configuration."
Condition="'$(_EnableMauiAspire)' != '' and '$(_MauiAspireSetByTargets)' != 'true'"/>
</Target>

<Target Name="_MauiXamlComputeInflator">
<ItemGroup>
<!-- Assign the default inflator to MauiXaml that don't have any -->
Expand Down Expand Up @@ -280,6 +290,9 @@
<MauiHybridWebViewSupported Condition="'$(MauiHybridWebViewSupported)' == ''">false</MauiHybridWebViewSupported>
<!-- FIXME: https://github.com/xamarin/xamarin-macios/issues/22065 -->
<MobileAggressiveAttributeTrimming Condition="'$(PublishAot)' == 'true' and '$(MobileAggressiveAttributeTrimming)' == ''">false</MobileAggressiveAttributeTrimming>
<_EnableMauiAspire Condition="'$(_EnableMauiAspire)' == '' and '$(Configuration)' == 'Debug'">true</_EnableMauiAspire>
<_EnableMauiAspire Condition="'$(_EnableMauiAspire)' == ''">false</_EnableMauiAspire>
<_MauiAspireSetByTargets>true</_MauiAspireSetByTargets>
Comment thread
jfversluis marked this conversation as resolved.
Outdated
</PropertyGroup>
<ItemGroup>
<RuntimeHostConfigurationOption Include="Microsoft.Maui.RuntimeFeature.IsIVisualAssemblyScanningEnabled"
Expand Down Expand Up @@ -318,6 +331,10 @@
Condition="'$(EnableDiagnostics)' != ''"
Value="$(EnableDiagnostics)"
Trim="true" />
<RuntimeHostConfigurationOption Include="Microsoft.Maui.RuntimeFeature.EnableMauiAspire"
Condition="'$(_EnableMauiAspire)' != ''"
Value="$(_EnableMauiAspire)"
Trim="true" />
</ItemGroup>
</Target>
</Project>
27 changes: 27 additions & 0 deletions src/Core/src/Hosting/Dispatching/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -35,8 +36,34 @@ public static MauiAppBuilder ConfigureDispatching(this MauiAppBuilder builder)

public static MauiAppBuilder ConfigureEnvironmentVariables(this MauiAppBuilder builder)
{
if (!RuntimeFeature.EnableMauiAspire)
{
return builder;
}

IDictionary environmentVariables = Environment.GetEnvironmentVariables();

#if ANDROID
const string androidEnvVarFilePath = "/data/local/tmp/ide-launchenv.txt";
Comment thread
jfversluis marked this conversation as resolved.

// For Android we read the environment variables from a text file that is written to the device/emulator
// If the file not exists, we will use the default environment variables which is less stable
if (OperatingSystem.IsAndroid() && System.IO.File.Exists(androidEnvVarFilePath))
Comment thread
jfversluis marked this conversation as resolved.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need the IsAndroid() check if it's inside #if ANDROID?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Probably not! Double safe!

{
var envVarLines = System.IO.File.ReadAllLines(androidEnvVarFilePath);

var fileEnvironmentVariables = envVarLines
.Select(line => line.Split('=', 2))
Comment thread
jfversluis marked this conversation as resolved.
Comment thread
jfversluis marked this conversation as resolved.
.ToDictionary(parts => parts[0], parts => parts[1]);

// Merge file environment variables into the existing environment variables
foreach (var kvp in fileEnvironmentVariables)
{
environmentVariables[kvp.Key] = kvp.Value;
}
}
#endif

string devTunnelId = environmentVariables["DEVTUNNEL_ID"]?.ToString() ?? string.Empty;

var variablesToInclude = new HashSet<string>
Expand Down
9 changes: 9 additions & 0 deletions src/Core/src/RuntimeFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static class RuntimeFeature
const bool SupportNamescopesByDefault = true;
const bool EnableDiagnosticsByDefault = false;
const bool IsMeterSupportedByDefault = true;
const bool EnableAspireByDefault = false;

#pragma warning disable IL4000 // Return value does not match FeatureGuardAttribute 'System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute'.
#if NET9_0_OR_GREATER
Expand Down Expand Up @@ -138,6 +139,14 @@ internal set
AppContext.SetSwitch($"{FeatureSwitchPrefix}.{nameof(EnableMauiDiagnostics)}", value);
}
}

#if NET9_0_OR_GREATER
[FeatureSwitchDefinition($"{FeatureSwitchPrefix}.{nameof(EnableMauiAspire)}")]
#endif
public static bool EnableMauiAspire => AppContext.TryGetSwitch($"{FeatureSwitchPrefix}.{nameof(EnableMauiAspire)}", out bool isEnabled)
? isEnabled
: EnableAspireByDefault;
Comment thread
jfversluis marked this conversation as resolved.

#pragma warning restore IL4000
}
}
Loading