2

I try to follow MS ex. on how to cross communication with two app in Windows IoT. Create and consume an app service

But in the code:

  public void Run(IBackgroundTaskInstance taskInstance)
{
    this.backgroundTaskDeferral = taskInstance.GetDeferral(); // Get a deferral so that the service isn't terminated.
    taskInstance.Canceled += OnTaskCanceled; // Associate a cancellation handler with the background task.

    var familyName = Windows.ApplicationModel.Package.Current.Id.FamilyName;

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

I get a NULL exception in

appServiceconnection = details.AppServiceConnection;

because that

 taskInstance.TriggerDetails as AppServiceTriggerDetails;

return NULL.

And i cant really google something up about it :(

  • My only guess is that there is something wrong with your configuration in package.appxmanifest. have you followed the instruction carefully. Have you fixed the class name in following line <uap:Extension Category="windows.appService" EntryPoint="MyAppService.Inventory"> with your class name? – George Mamaladze Sep 24 '16 at 20:49
1

I'm doing some work related to IOT on Windows 10, but not an expert.

Firstly (what i managed to grasp on the subject) is that the Run(IBackgroundTaskInstance taskInstance) can be triggered by different parties.

When you trigger it in Visual Studio as debug you will get the aforementioned exception. You should do a check as @goobering mentions. The part to notice is that the actual client has to trigger the task. In the client use AppServiceConnection for example:

         var service = new AppServiceConnection();

        // Here, we use the app service name defined in the app service provider's Package.appxmanifest file in the <Extension> section.
        service.AppServiceName = "//TODO";

        // Use Windows.ApplicationModel.Package.Current.Id.FamilyName within the app service provider to get this value.
        service.PackageFamilyName = "//TODO";

        var status = await service.OpenAsync();
        if (status != AppServiceConnectionStatus.Success)
        {
            // Somethings wrong ...
        }
        else
        {
            // Use service
        }

Then the background task will be triggered with the connection details. Sadly this IOT does not have many verbose and satisfying examples. What I am trying to achieve is have a background web service used from e.g. a desktop and also it notifies about the changes.

You can also make an in process background task (added in some version of IOT). Then the communication is simplified assuming you dont the features missing from the out of process background tasks. From within the headed app you can also trigger the actual background task if its not already running (not an always "ON" task). See example.

0

I haven't done enough UWP/IoT development to be fully competent on this one, but looking at this post on mtaulty.com it appears that it's not necessarily unusual for this value to be null. He uses a basic check (if (appService != null)...) to establish that there's actually something there before continuing.

BackgroundTaskDeferral deferral;

public async void Run(IBackgroundTaskInstance taskInstance)
{
    this.deferral = taskInstance.GetDeferral();
    taskInstance.Canceled += OnCancelled;

    var appService = taskInstance.TriggerDetails as AppServiceTriggerDetails;

    if (appService != null)
    {
        // we should do more validation here
        if (appService.Name == "flickrphoto")
        {
            appService.AppServiceConnection.RequestReceived += OnAppServiceRequestReceived;
        }
    }
    else
    {
        this.deferral.Complete();
        this.deferral = null;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.