Search code examples
c#exceptionuwpbackground-task

System.UnauthorizedAccessException in Background Task


Problem: When I call AudioDeviceModulesManager(id) from a background task it throws System.UnauthorizedAccessException

When I google the problem most of the hits are to people trying to access a specific file there program doesn't have access to. I understand that abstractly I'm doing the same thing but I have no idea what rights to give a background task, or how I would do it, to allow it to enumerate AudioDeviceModules.

Links:


Solution

  • When I call AudioDeviceModulesManager(id) from a background task it throws System.UnauthorizedAccessException

    According to Configure and query audio device modules:

    In order to use the audio device module APIs shown in this article, you must specify the restricted audioDeviceConfiguration capability in your app package manifest.

    So that you need to specify the restricted capability audioDeviceConfiguration in your app package manifest as follows:

    <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
    ...
    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    IgnorableNamespaces="uap mp rescap">
    
    <Capabilities>
      <Capability Name="internetClient" />
      <rescap:Capability Name="audioDeviceConfiguration"/>       
    </Capabilities>
    

    Pay attention that only Microsoft partners and those who work with a device vendor may request access to this capability for store submission. This is because AudioDeviceModulesManager allows an application to access to all audio effects on a given system. Potentially, the audio effects can be set to negatively impact audio performance on the device.

    More details please reference App capability declarations.