BackgroundTaskBuilder.Register throws HRESULT: 0x00700B7 on multiple task registration

Brian Henning 0 Reputation points
2024-09-30T17:52:54.3966667+00:00

A bit of background: I am working in an ecosystem where independent devices on a network ask a central server for things to do. Lately I've been working on a new kind of device that itself does not have a network connection, and connects to a PC with USB. So I've put together an app to act as the client and fetch instructions from the server and feed them to the device.

The app performs these basic operations:

  1. Every second, ask the server for instructions with an HTTP exchange
  2. Talk to the device over USB (CDC, if it's relevant)
  3. Sometimes, if the device did a thing, tell the server about it with another HTTP exchange

The app needs to continue this operation whether or not it is in the foreground. I have collected these behaviors into two background task classes; one that handles the HTTP communication and one that handles communication with the USB device. Regardless of which task I attempt to register first, the first task registration succeeds while the attempt to register the second task results in an exception with the following text:

Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7)

Also, the first (successfully-registered) task's Run method is never called, before or after a call to its trigger's RequestAsync method (which returns Allowed).

The two tasks are registered in the appxmanifest:

<Package>
[ ... ]
 <Extensions>
  <Extension Category="windows.activatableClass.inProcessServer">
   <InProcessServer>
	<Path>UI.exe</Path>
	<ActivatableClass ThreadingModel="MTA" ActivatableClassId="BackgroundTasks.BackgroundTaskOne" />
	<ActivatableClass ThreadingModel="MTA" ActivatableClassId="BackgroundTasks.BackgroundTaskTwo" />
   </InProcessServer>
  </Extension>
 </Extensions>
[ ... ]
 <Applications>
  <Application Id="App" [...] >
   [...]
   <Extensions>
    <Extension Category="windows.backgroundTasks" EntryPoint="BackgroundTasks.BackgroundTaskOne">
	 <BackgroundTasks>
	  <Task Type="general" />
	 </BackgroundTasks>
    </Extension>
    <Extension Category="windows.backgroundTasks" EntryPoint="BackgroundTasks.BackgroundTaskTwo">
	 <BackgroundTasks>
	  <Task Type="general" />
	 </BackgroundTasks>
    </Extension>
   </Extensions>    
  </Application>
 </Applications>
</Package>

Note that I openly do not understand ThreadingModel very well. I also tried STA and both, the other options suggested by Intellisense, and the behavior didn't change.

Each task class has the following signature (e.g.):

public sealed class BackgroundTaskOne : IBackgroundTask 

and uses identical code to attempt the registration:


var requestTask = BackgroundExecutionManager.RequestAccessAsync().AsTask();
requestTask.Wait();
var builder = new BackgroundTaskBuilder();
builder.Name = GetType().Name;
builder.TaskEntryPoint = GetType().FullName;
// builder.IsNetworkRequested = true;
builder.SetTrigger(trigger);
try {
    registration = builder.Register();
    return true;
} catch(Exception ex) {
    Log.Logger.Error($"BackgroundTaskBuilder.Register: {ex.Message}");
    Log.Logger.Information($"Name: {builder.Name}");
    Log.Logger.Information($"TaskEntryPoint: {builder.TaskEntryPoint}");
    return false;
}

To demonstrate and isolate the issue, I've uploaded an MCRE here. Click buttons, see errors in the debug console.

Apparently I don't understand what constitutes uniqueness in a background task registration; I would've presumed the combination of Name and perhaps TaskEntryPoint would serve to uniquely identify a background task. The exception, however, seems to suggest I'm trying to do the same thing twice.

Questions:

  1. Should an app be able to register more than one BackgroundTask?
  2. If so, what do I have wrong such that I get this exception?
  3. Why doesn't the Run method on either task ever get called?
Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,487 questions
Universal Windows Platform (UWP)
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.