r.t.s.
r.t.s.

Reputation: 585

Background Task Registration Fails

I want to capture the raw push messages in background process. When I try to register it I get the following exception:

HResult -2147221164
Message "Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))"

The code is as follows:

    public sealed class NotificationsTask : IBackgroundTask {
    static private String sName = typeof(NotificationsTask).Name;
    static private readonly String TAG = sName;
    public void Run(IBackgroundTaskInstance taskInstance) {
      RawNotification notification = (RawNotification) taskInstance.TriggerDetails;
      String msg = notification.Content;
    }
    async static public void Register() {
      var Status = BackgroundExecutionManager.GetAccessStatus();
      Boolean OKToRegister = true;
      switch (Status) {
        case BackgroundAccessStatus.DeniedByUser:
        case BackgroundAccessStatus.Unspecified:
        case BackgroundAccessStatus.DeniedBySystemPolicy:
          OKToRegister = false;
          Status = await BackgroundExecutionManager.RequestAccessAsync();
          switch (Status) {
            case BackgroundAccessStatus.AllowedSubjectToSystemPolicy:
            case BackgroundAccessStatus.AlwaysAllowed:
              OKToRegister = true;
              break;
          }
          break;
      }
      if (OKToRegister) {
        Boolean found = false;
        foreach (var task in BackgroundTaskRegistration.AllTasks) {
          if (task.Value.Name == sName) {
            found = true;
            break;
          }
        }
        if (!found) {
          try {
            var builder = new BackgroundTaskBuilder();
            builder.TaskEntryPoint = typeof(NotificationsTask).FullName;
            builder.Name = sName;
            builder.SetTrigger(new PushNotificationTrigger());
            builder.Register();
          } catch (Exception e) {
            PersistLog.e(TAG, "Register:" + e);
          }
        }
      }
    }

    static public void UnRegister() {
      BackgroundExecutionManager.RemoveAccess();
      foreach (var task in BackgroundTaskRegistration.AllTasks) {
        if (task.Value.Name == sName) {
          task.Value.Unregister(true);
          break;
        }
      }
    }
}

I have tried a timer trigger, same result. In my test case I am always calling the UnRegister method before calling the Register method. It does not find a previously registered class.

I tried to Add to the manifest file with:

    <Extension Category="windows.backgroundTasks" EntryPoint="General.NotificationsTask">
      <BackgroundTasks>
        <Task Type="pushNotification" />
      </BackgroundTasks>
    </Extension>

But then I get the error:

Validation error. error 80080204: App manifest validation error: Line 33, Column 12, Reason: If it is not an audio background task, it is not allowed to have EntryPoint without ActivatableClassI

Upvotes: 1

Views: 546

Answers (1)

r.t.s.
r.t.s.

Reputation: 585

I should have loaded the Sample Solution into Visual Studio first! My problem was that I did not put my tasks in their own Assembly that could be registered and dynamically loaded as COM objects.

I do not remember seeing this in the documentation ... but it's obvious when you review the structure of the Background Task Samples project.

Upvotes: 2

Related Questions