Skip to content

Commit

Permalink
[net7.0] Initial code for replacing the deployment manager (#12745)
Browse files Browse the repository at this point in the history
* Initial code for replacing the deployment manager

* better code

* bit better and avoids an unneccessary creation

* more nice code

* More coolness

* better logging

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
  • Loading branch information
github-actions[bot] and mattleibow committed Jan 18, 2023
1 parent 79815e3 commit 90ad642
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
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

0 comments on commit 90ad642

Please sign in to comment.