HostBuilder and Host for Windows App SDK #4358
Replies: 4 comments 6 replies
-
I like this a lot! This is something that is sorely missed in the current App SDK experience and I would love to see it show up in the SDK proper some day to align with other modern .NET project types. My one question is how feasible is it to allow the elimination of |
Beta Was this translation helpful? Give feedback.
-
There hasn't been much activity on this discussion here, it generated a lot more on Reddit. Is this just something that WinAppSdk devs don't care about? |
Beta Was this translation helpful? Give feedback.
-
I'm a bit confused with how much of the code was re-written for the Maybe I'm missing something? |
Beta Was this translation helpful? Give feedback.
-
In the executable assembly: <PropertyGroup>
<!-- Use our own Main entry point so we can control the HostBuilder and make the EntityFramework work -->
<DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
<StartupObject>Oxygen.Editor.MainProgram</StartupObject>
</PropertyGroup> In the MainProgram.cs: {
[DllImport("Microsoft.ui.xaml.dll")]
private static extern void XamlCheckProcessRequirements();
private static HostApplicationBuilder CreateBuilder(string[] args)
{
var builder = new HostApplicationBuilder(args);
builder.Configuration.SetBasePath(finder.ProgramData)
.AddJsonFile("appsettings.json")
.SetBasePath(finder.LocalAppData)
.AddJsonFile("LocalSettings.json", true)
.SetBasePath(finder.ProgramData)
.AddJsonFile(
$"{Assembly.GetAssembly(typeof(ProjectBrowserSettings))!.GetName().Name}/Config/ProjectBrowser.config.json");
builder.Services....
// TODO: add services
builder.Services.AddSqlite<PersistentState>($"Data Source={dbPath}; Mode=ReadWriteCreate");
return builder;
}
[STAThread]
private static void Main(string[] args)
{
var builder = CreateBuilder(args);
var host = builder.Build();
Ioc.Default.ConfigureServices(host.Services);
host.StartAsync()
.GetAwaiter()
.GetResult();
void OnAppOnUnhandledException(object sender, UnhandledExceptionEventArgs args)
{
// TODO: handle unhandled exceptions
args.Handled = false;
}
XamlCheckProcessRequirements();
WinRT.ComWrappersSupport.InitializeComWrappers();
Application.Start(
_ =>
{
try
{
var context = new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread());
SynchronizationContext.SetSynchronizationContext(context);
var app = new App();
app.UnhandledException += OnAppOnUnhandledException;
}
catch (Exception ex)
{
Debug.WriteLine($"Error application start callback: {ex.Message}.");
}
});
}
} Voila. |
Beta Was this translation helpful? Give feedback.
-
@michael-hawker asked me to post this for community feedback.
I have created a
HostBuilder
andHost
for Windows App SDK Desktop applications. The repository is at sharpninja/WindowsAppSdkHost and includes the complete WindowsAppSdkHost and WindowsAppSdkHostBuilder implementations which are based on the Microsoft.Extensions.Hosting.HostBuilder and Microsoft.Extensions.Hosting.Internal.Host implementations by Microsoft. The repository contains a complete Desktop application that references and uses both the builder and host.What does it do?
Allows hosting a Windows App SDK Application in an
IHost
that manages the lifecycle of the hosted Application.Usage
(Convert existing project or the default template's output)
<DefineConstants>DISABLE_XAML_GENERATED_MAIN</DefineConstants>
in the mainPropertyGroup
of your applications project file.CommunityToolkit.Extensions.Hosting.WindowsAppSdk
Program.cs
to the root of your application project.Program.cs
:Program.cs
as the startup object by adding<StartupObject>HostedWindowsAppSdk.Program</StartupObject>
to your project file.CancelableApplication
as the base class of your application by modifying yourApp.xaml
:Notes
The
WindowsAppSdkHost
uses several features of the Microsoft.Extensions ecosystem:DefaultHostBuilder
.CancellableApplication
with dependency injection.StartAsync
method of theWindowsAppSdkHost
.ILogger
. TheIlogger
can be obtained from the staticServices
property of theCancellableApplication
after building the app.TODO
I need help getting the project template formatted correctly for
dotnet new
.Beta Was this translation helpful? Give feedback.
All reactions