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

[net7.0] Initial code for replacing the deployment manager #12745

Merged
merged 6 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .nuspec/Microsoft.Maui.Controls.SingleProject.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<WindowsPackageType Condition=" '$(WindowsPackageType)' == '' and '$(EnableMsixTooling)' == 'true' and '$(OutputType)' == 'WinExe' ">MSIX</WindowsPackageType>
<WindowsAppSDKSelfContained Condition=" '$(WindowsAppSDKSelfContained)' == '' and '$(WindowsPackageType)' == 'None' and '$(OutputType)' == 'WinExe' ">true</WindowsAppSDKSelfContained>
<WindowsAppSdkBootstrapInitialize Condition=" '$(WindowsAppSdkBootstrapInitialize)' == '' and '$(EnableMsixTooling)' == 'true' and '$(OutputType)' != 'WinExe' ">false</WindowsAppSdkBootstrapInitialize>
<!-- We have our own manager to avoid crashing the app -->
<WindowsAppSdkDeploymentManagerInitialize>false</WindowsAppSdkDeploymentManagerInitialize>
<PublishAppXPackage Condition=" '$(PublishAppXPackage)' == '' and '$(EnableMsixTooling)' == 'true' and '$(WindowsPackageType)' == 'MSIX' ">true</PublishAppXPackage>
<PublishReadyToRun Condition=" '$(PublishReadyToRun)' == '' and '$(Configuration)' == 'Release' and '$(OutputType)' != '' and '$(OutputType)' != 'Library' ">true</PublishReadyToRun>
<_SingleProjectRIDRequired Condition="'$(OutputType)' == 'WinExe'">true</_SingleProjectRIDRequired>
Expand Down
50 changes: 50 additions & 0 deletions src/Core/src/Platform/Windows/DeploymentManagerAutoInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;
using Microsoft.Windows.ApplicationModel.WindowsAppRuntime;

namespace Microsoft.Maui;

internal static class DeploymentManagerAutoInitializer
{
#pragma warning disable CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
[ModuleInitializer]
#pragma warning restore CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
[EditorBrowsable(EditorBrowsableState.Never)]
internal static void AccessWindowsAppSDK()
{
try
{
var options = new DeploymentInitializeOptions();
Result = DeploymentManager.Initialize(options);
}
catch (Exception ex)
{
Result = new DeploymentResult(DeploymentStatus.Unknown, ex);
}
}

public static DeploymentResult Result { get; private set; } = null!;

public static void LogIfFailed(IServiceProvider services)
{
if (Result?.Status == DeploymentStatus.Ok)
return;

var logger = services.CreateLogger("Microsoft.Maui.DeploymentManagerAutoInitializer");
if (logger is null)
return;

var error = Result?.ExtendedError;
if (error is null)
{
logger.LogError($"Unknown WindowsAppRuntime.DeploymentManager.Initialize error.");
}
else
{
var hresult = string.Format("0x{0:X}", error.HResult);
logger.LogError(error, "WindowsAppRuntime.DeploymentManager.Initialize error ({HResult}): {ErrorMessage}", hresult, error.Message);
}
}
}
2 changes: 2 additions & 0 deletions src/Core/src/Platform/Windows/MauiWinUIApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ protected override void OnLaunched(UI.Xaml.LaunchActivatedEventArgs args)

Services = applicationContext.Services;

DeploymentManagerAutoInitializer.LogIfFailed(Services);

Services.InvokeLifecycleEvents<WindowsLifecycle.OnLaunching>(del => del(this, args));

Application = Services.GetRequiredService<IApplication>();
Expand Down