How to properly register App Service Background Task in Unity UWP app

0

I want to achieve app-to-app two-way communication between a UWP XAML app and a UWP Unity app using App-Services. In the first step I am trying to create an App-Service BackgroundTask in the Unity app and then consume it in the XAML App. I have been following Microsoft's Documentation on creating App-Services and creating and registering Background Tasks. I have edited the Package.appxmanifest file to include the AppService. Is this even possible using the IL2CPP backend in Unity? Or am I simply getting something wrong with the registration or the App Service entry point?

My code to consume an App-Service seems to be working fine, but I am at a loss as to how to register a Background Task in Unity properly. I have a class "AppServiceHandler" which contains methods to Run a Background Task and a method to register the task at Startup (at least I think that is what it should be doing). This class is not called by any Object in the Scene and only relies on the Registration at Startup.

public sealed class AppServiceHandler: IBackgroundTask
{
    private BackgroundTaskDeferral backgroundTaskDeferral;
    private AppServiceConnection appServiceConnection;

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        // Get a deferral so that the service isn't terminated.
        backgroundTaskDeferral = taskInstance.GetDeferral();

        // Associate a cancellation handler with the background task.
        taskInstance.Canceled += OnTaskCanceled;

        // Retrieve the app service connection and set up a listener for incoming app service requests.
        var details = taskInstance.TriggerDetails as AppServiceTriggerDetails;
        appServiceConnection = details.AppServiceConnection;
        appServiceConnection.RequestReceived += OnRequestReceived;
    }

    private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
    {
        // Get a deferral because we use an awaitable API below to respond to the message
        // and we don't want this call to get canceled while we are waiting.
        var messageDeferral = args.GetDeferral();

        ValueSet message = args.Request.Message;
        ValueSet returnData = new ValueSet();

        string targetObject = message["targetObject"] as string;
        string command = message["command"] as string;
        string commandParameters = message["commandParameters"] as string;

        GameObject targetGameObject = GameObject.Find(targetObject);

        switch (command)
        {
            ...
        }

        try
        {
            await args.Request.SendResponseAsync(returnData);
        }
        catch
        {
            Debug.Log("Unable to send app service response.");
        }
        finally
        {
            messageDeferral.Complete();
        }

    }

    private void OnTaskCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
    {
        if (backgroundTaskDeferral != null)
        {
            // Complete the service deferral.
            backgroundTaskDeferral.Complete();
        }
    }

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    static void RegisterTask()
    {
        bool taskRegistered = false;
        string taskName = "AppServiceHandler";

        foreach(var task in BackgroundTaskRegistration.AllTasks)
        {
            if(task.Value.Name == taskName)
            {
                Debug.Log("Task is already registered!");
                taskRegistered = true;
                break;
            }
        }

        if (!taskRegistered)
        {
            var builder = new BackgroundTaskBuilder();

            builder.Name = taskName;
            builder.TaskEntryPoint = "HoloMetrix.Net.AppService.AppServiceHandler";
            BackgroundTaskRegistration task = builder.Register();
        }
    }
}

When I build the solution and run the app from VS I get the following Error at Startup:

Exception thrown at 0x00007FF84DE49129 in App Service Example.exe: Microsoft C++ exception: Il2CppExceptionWrapper at memory location 0x0000001598AFDBE0.
ArgumentException: Value does not fall within the expected range.
  at Windows.ApplicationModel.Background.BackgroundTaskBuilder.Register () [0x00000] in <00000000000000000000000000000000>:0 
  at AppService.AppServiceHandler.RegisterTask () [0x00000] in <00000000000000000000000000000000>:0 

(Filename: currently not available on il2cpp Line: -1)

And when I attempt to consume the App Service from the XAML app Unity throws:

Exception thrown at 0x00007FF84DE49129 (KernelBase.dll) in backgroundTaskHost.exe: WinRT originate error - 0x80040154 : 'Klasse nicht registriert'. (Class not registered)
c#
unity3d
uwp
background-task
asked on Stack Overflow Aug 27, 2019 by ChrisM

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0