Search code examples
c#push-notificationuwpbackground-processbackground-task

UWP network communication in the background


I'm trying to go through the official tutorial for background task network in UWP titled Network communications in the background. However, since the tutorial is poorly ported from C++ code, the code, after being modified to be able to compile, fails at instantiating the ControlChannelTrigger object with an AccessDenied exception.

The exception is thrown at the following line.

channel = new ControlChannelTrigger(channelId, serverKeepAliveInterval,
                                    ControlChannelTriggerResourceType.RequestHardwareSlot);

I read the comment about the lock screen and tried to add the app to the lock screen (even though this should no longer be a limitation), but the error persists.

I'm trying to maintain a persistent WebSocket connection with my own server to allow push notifications (I am aware of WNS, but it is not the right choice for my use-case).

My problem is similar to one in the related question ControlChannelTrigger in UWP Windows 10 access denied.

For simplicity, I am implementing everything as an in-process background task.

Can anyone suggest a solution or a workaround? Could the tutorial be outdated?


Solution

  • Unsurprisingly, I found the answer to my own question again.

    The registered background task cannot be an in-process task and must be registered as an out-of-process task.

    As for the Windows.Networking.Sockets.WebSocketKeepAlive entry point, I made a workaround by creating a background task that contained the Windows.Networking.Sockets.WebSocketKeepAlive and called its Run(…) method.