Support for Background Tasks in WPF desktop apps (.NET 5 and WinUI 3)

Jos Huybrighs 336 Reputation points

I am currently experimenting with the latest possibilities in Visual Studio 19 preview 2, to develop a packaged WPF desktop app that is using WinUI 3 for the UI.
I know that all of this is not yet intended for production use but I would like to understand whether somewhere in 2021 this could be a better approach for developing apps for the Windows Store instead of UWP, DesktopBridge, or WPF with XamlIslands.

In particular I would like to know whether WPF desktop apps on .NET 5 and WinUI 3 are supporting or eventually will support Background Tasks in the way they are supported on UWP apps. The beauty of the latter is that registration/deregistration of background tasks on installation/update/deinstallation of the app works seamlessly. It would be great if this would also work with a WPF Desktop app.

I experimented a little bit with this and discovered that I can write code to register an out-of-process background task using the standard provisioned api (Microsoft.Windows.SDK.NET.dll). However, it is not possible to reference a c# Windows Runtime Component that would implement the background task. There are currently only 2 type of Windows Runtime Component templates in Visual Studio 19, prev2:

  • Windows Runtime Component (WinUI in UWP) for a managed (c#) .winmd, and
  • Windows Runtime Component (WinUI) for a unmanaged (c++) .winmd.

None of them can be referenced / included in a .NET5 app.

My question basically is: does it make any sense to continue working in this direction or should I have to look for alternative ways to have some type of background tasks?

Universal Windows Platform (UWP)
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,289 questions
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
602 questions
{count} votes

Accepted answer
  1. Jos Huybrighs 336 Reputation points

    It turns out that it is indeed possible to create a WinUI 3 desktop app that supports Background Tasks in the way that this is possible with a pure UWP or a UWP desktop bridge app.

    Unfortunately Visual Studio 2019 prev.2 doesn't support this immediately with any of the available project templates. Luckily enough BrunoBlanes (see Question: Is it possible to create an out-of-process background task in a Desktop application as is with UWP?) explained me how to do this.

    Because not everything is obvious I will describe the various steps to take.

    This is what you have to do:

    1. Create a Windows Runtime Component (WRC) project to implement the background task. Do this in Visual Studio 2019 by choosing the c# Windows Runtime Component (WinUI in UWP) project template and selecting 10.0.19041.0 as target version. Add your background task class (inheriting from IBackgroundTask).
    2. Create a UWP hosting app for the above WRC. Choose the c# Blank App(WinUI in UWP) project template and select 10.0.19041.0 as target version.
    3. Do the following in the UWP hosting app project:
      1. Rename App.xaml and App.xaml.cs to Main.xaml and Main.xaml.cs en rename the App class to Main (in both Main.xaml and Main.xaml.cs).
      2. Delete MainPage.xaml and MainPage.xaml.cs.
      3. In Main.xaml.cs: Change using Microsoft.UI.Xaml to using Windows.UI.Xaml, keep only an empty Main class constructor, and delete all other usings.
      4. Delete the <Application.Resources> entry completely in Main.xaml.
      5. Add a reference to the above WRC project.
    4. Close the solution and remove the Microsoft.WinUI reference by deleting the WinUI PackageReference in both the WRC and the hosting project file.
    5. Open the solution. You then might still have to remove the Microsoft.ApplicationModel.Resources.winmd reference and all the VCLib references in both projects before the WinUI reference actually disappears.

    You are now good to go.

    1. Add the hosting app to your desktop packaging project
    2. Add the necessary extensions in the Package.appxmanifest file of the packaging project
    3. Do the necessary background task registrations in the code of the Desktop App project.
    1 person found this answer helpful.
    No comments

1 additional answer

Sort by: Most helpful
  1. Angela Zhang 1 Reputation point

    C#/WinRT is working to provide support for this scenario. With C#/WinRT you can author WinRT types in C# .NET 5. The background task scenario currently involves some project hacks and this is ongoing work that will be finished in the upcoming few weeks. Here's a sample of the current state of how to author a background task and consume it from a .NET 5 WPF app: [][1]