{"id":15059814,"url":"https://github.com/abdes/winui-generic-host","last_synced_at":"2025-08-27T01:34:59.774Z","repository":{"id":207753338,"uuid":"719092709","full_name":"abdes/winui-generic-host","owner":"abdes","description":"A WinUI 3 visual studio solution for an application that uses Microsoft.Extensions.Hosting.","archived":false,"fork":false,"pushed_at":"2024-01-26T13:13:14.000Z","size":143,"stargazers_count":13,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T06:54:38.845Z","etag":null,"topics":["configuration","csharp","dependency-injection","generic","host","lifecycle","netframework","windows","winui","winui3"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/abdes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-11-15T12:45:27.000Z","updated_at":"2025-03-18T13:39:49.000Z","dependencies_parsed_at":"2024-01-26T14:30:05.193Z","dependency_job_id":"3b48df99-af45-486d-8faf-19c579f93b3d","html_url":"https://github.com/abdes/winui-generic-host","commit_stats":null,"previous_names":["abdes/winui-generic-host"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abdes%2Fwinui-generic-host","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abdes%2Fwinui-generic-host/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abdes%2Fwinui-generic-host/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abdes%2Fwinui-generic-host/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abdes","download_url":"https://codeload.github.com/abdes/winui-generic-host/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248166308,"owners_count":21058475,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["configuration","csharp","dependency-injection","generic","host","lifecycle","netframework","windows","winui","winui3"],"created_at":"2024-09-24T22:48:14.614Z","updated_at":"2025-04-10T05:41:52.295Z","avatar_url":"https://github.com/abdes.png","language":"C#","readme":"# Generic Host for WinUI applications\n\nThe [.Net Hosting\nextensions](https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host)\nlook like they are only for ASP .Net but they are not. The documentation now\nintroduces them as part of the [.Net\nfundamentals](https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host)\nand they can very well be used in any scenario including console and desktop\napplications.\n\nA host is an object that encapsulates an app's resources and lifetime\nfunctionality, such as:\n\n- Dependency injection (DI)\n- Configuration (in-memory, json, secrets, environment variables, command line)\n- Logging\n- Diagnostics\n- Different types of File Providers\n- App startup/shutdown and IHostedService implementations\n\n➡ What if we could leverage all of that in a WinUI application?\n\nYou really don't need a dozen of Nugets from many maintained or no longer\nmaintained sources. All of that can be done with just the .Net framework. Let's\nsee how.\n\n## Before we start\n\nThe starting point for the application will be the basic WinUI application with\na custom `Main` entry point. Not that it is absolutely required, but it will\ngive us full control of the setup of the Generic Host environment and will make\nthe WinUI subsystem just another service for which we manage the lifecycle.\n\nSee \u003chttps://github.com/abdes/winui-override-main\u003e for the basics of how to\noverride the default entry point.\n\n## A hosted service for WinUI\n\nA typical WinUI application runs in a UI thread, assumed to be the main thread,\nand for which the dispatched is initialized in the `Main` entry point.\n\nWhat we want is to have that thread be a background service, hosted by the\ngeneric host, along with the other application services. This will give us the\nadvantage of managing the UI just as another service, and of course getting all\nthe goodies of the Generic Host listed above.\n\nThe implementation, extensively documented in the source code, adds a\n[UserInterfaceHostedService](Hosting/Desktop/WinUI/UserInterfaceHostedService.cs)\nwhich runs the ['UI Thread'](Hosting/Desktop/WinUI/UserInterfaceThread.cs)\nas a background service. We can decide, based on the options in the\n[HostingContext](Hosting/Desktop/WinUI/HostingContext.cs) if we want the\nlifetime of that 'UI Thread' to be linked to the application lifetime or not. In\nother words, we can decide of termination of the 'UI Thread' results in\ntermination of the application or not, and vice versa.\n\n## An extension method for the builder\n\nTo simplify the setup of the User Interface hosted service, an [extension\nmethod](Hosting/Desktop/WinUI/HostingExtensions.cs) to the host builder is\nprovided.\n\nThis greatly simplifies the host building as it is now just a matter of calling\nthat extension method.\n\n```csharp\n    private static void Main(string[] args)\n    {\n        // Use a default application host builder, which comes with logging,\n        // configuration providers for environment variables, command line,\n        // appsettings.json and secrets.\n        var builder = Host.CreateApplicationBuilder(args);\n\n        // You can further customize and enhance the builder with additional\n        // configuration sources, logging providers, etc.\n\n        // Setup and provision the hosting context for the User Interface\n        // service.\n        ((IHostApplicationBuilder)builder).Properties.Add(\n            key: HostingExtensions.HostingContextKey,\n            value: new HostingContext() { IsLifetimeLinked = true });\n\n        // Add the WinUI User Interface hosted service as early as possible to\n        // allow the UI to start showing up while you continue setting up other\n        // services not required for the UI.\n        var host = builder.ConfigureWinUI\u003cApp, MainWindow\u003e().Build();\n\n        // Finally start the host. This will block until the application\n        // lifetime is terminated through CTRL+C, closing the UI windows or\n        // programmatically.\n        host.Run();\n    }\n```\n\n## Why the UI hosted service does not create the MainWindow?\n\nSimply because not all applications are single window applications and it is not\nobvious in a multi-window application which window will terminate the UI when\nclosed. Therefore, it is much more preferable to continue having the\n`Application` responsible for managing its `Window`s.\n\n## Can this be adapted to WPF, etc. ?\n\nYes. Simply implement the classes inside WinUI namespace for any other UI\nframework.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabdes%2Fwinui-generic-host","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabdes%2Fwinui-generic-host","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabdes%2Fwinui-generic-host/lists"}