C#C
C#2y ago
apcr

How to start a background service using Package.appxmanifest?

can someone please help me to add a Background Service to my Avalonia UI app that's delivered through an appx?

I have two projects in my solution:
  1. MyApp.UI - Avalonia UI project
  2. MyApp.UI.Installer - Application Packaging Project for creating an APPX installer
Here's what I tried initially:

  1. First, in MyApp.UI, I created a file called PushNotificationBackgroundService.cs:
    public class PushNotificationBackgroundService : IBackgroundTask
    {
     // Entry point that should eventually be called
     public async void Run(IBackgroundTaskInstance taskInstance) {...}
    }
  2. Then, in MyApp.UI.Installer, I added a reference to MyApp.UI.
  3. After that, in MyApp.UI.Installer, I edited the Package.appxmanifest file to add an Extension for the existing application:
    <Applications>
     <Application Id="App"
                  Executable="$targetnametoken$.exe"
                  EntryPoint="$targetentrypoint$">
         <Extensions>
             <Extension Category="windows.backgroundTasks" EntryPoint="$targetentrypoint$.PushNotificationBackgroundService">
                 <BackgroundTasks>
                     <Task Type="pushNotification"/>
                 </BackgroundTasks>
             </Extension>
         </Extensions>
     </Application>
    </Applications>
The package compiles, the app installs and opens, but the PushNotificationBackgroundService isn't visible anywhere. There are no errors in the Event Viewer, and it's not showing up in processes. I even tried writing to a log file to see if it was running, but there's no file.

What am I doing wrong? Are there other ways to do this without using MSIX? Maybe directly from Program.cs?

I tried different values for the EntryPoint attribute:
  • EntryPoint="$targetentrypoint$.PushNotificationBackgroundService"
  • EntryPoint="MyApp.UI.PushNotificationBackgroundService"
  • EntryPoint="PushNotificationBackgroundService"
Nothing seems to work
Was this page helpful?