2
votes

I am trying to implement Background Tasks in my project. So I added a Windows Runtime Component Project and given reference to it from my main Windows Phone 8.1 silverlight project. But When I calling the below function I am getting an exception

"A first chance exception of type 'System.Exception' occurred in mscorlib.ni.dll. Additional information: Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))"

        // Applications must have lock screen privileges in order to receive raw notifications
        BackgroundAccessStatus backgroundStatus = await BackgroundExecutionManager.RequestAccessAsync();

        // Make sure the user allowed privileges
        if (backgroundStatus != BackgroundAccessStatus.Denied && backgroundStatus != BackgroundAccessStatus.Unspecified)
        {
            OpenChannelAndRegisterTask();
        }
        else
        {
            // This event comes back in a background thread, so we need to move to the UI thread to access any UI elements
            Dispatcher.BeginInvoke(() =>
            {
                Debug.WriteLine("Lock screen access is denied");
            });
        }

Why this happens? Later I create a sample Windows phone project and the above code is working fine. What will be the reason for this strange issue?

Please help me to resolve this issue if you have any clue about it.

2

2 Answers

5
votes

Background tasks have to be registered ("declared") in the Package.appxmanifest using their class name.

Double click your appxmanifest file, got to Declarations and make sure you have a) declared the background task there and b) it has the corrected class name (like NamespaceName.BackgroundTaskClassName) entered under Entry point.

RequestAccessAsync for me failed with the exact same error message you are getting because I forgot to register my background tasks.

(Note for readers who are targeting Windows 10 instead of 8(.1): the answer from Frank Sposaro does not apply to Windows 10 anymore, as the MSDN link he provides correctly documents.)

2
votes

According to MSDN you should be running the RequestAccessAsync from the UI thread. You may want to ensure that's the case, perhaps with a Dispatch if needed. I've seen it throw a variety of exceptions, perhaps REGDB_E_CLASSNOTREG is one of them.