UWP/C# 'Access is denied' Exception when setting Geofence

1

I have a .NET 5 Console Application for which I used following the documentation the '.NET 5: Use the Target Framework Moniker option'. This means my project file looks like this at the moment:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows10.0.18362</TargetFramework>
  </PropertyGroup>

</Project>

I then tried access the Location Api using new Geolocator().GetGeopositionAsync() which works as expected. To my understanding, there are certain UWP APIs that I can only use if my app has a package identity. So I created a an Application Packaging Project that references as application my Console App. I followed the documentation for packaging the whole project as msix package. This now works well and I can still access the current geoposition as expected.

However, I followed the documentation for setting up a geofence in UWP and as soon as I try to register a geofence or even register an event handler for triggered geofences I get a

One or more errors occurred. (Access is denied. (0x80070005 (E_ACCESSDENIED)))

exception. My capabilities in the manifest file are defined as following:

  <Capabilities>
    <Capability Name="internetClient" />
    <rescap:Capability Name="runFullTrust" />
    <DeviceCapability Name="location"/>
  </Capabilities>

Before I try setting a geofence or registering an event handler, I always call await Geolocator.RequestAccessAsync() and only continue execute the action if this method returns Allowed which is every time. This means the answer to this question does not help me.

Is there any possibility to make sure that there is nothing wrong with how I defined my capabilities? For example, is there a way to check my capabilities at runtime? So far, I did not find anything on that topic. Or is there a problem with my principal assumption that as soon as my Console App has a package id because it's packaged as msix, I can access features like Geofencing that would not be accessible to me otherwise?

After having checked that I have access to the location with a call to await Geolocator.RequestAccessAsync() I for example try to execute the following code:

GeofenceMonitor.Current.GeofenceStateChanged += this.OnGeofenceStateChanged;

and immediately receive the exception.

EDIT: I have now also tried to use the same Code in a UWP App. There, it works like a charm and I noticed that I get a Pop-Up when RequestLocation is executed asking me to grant the app permission to access the location. I have never received a similar Pop-Up in my console application. I thought this might be because of a lacking UI Thread.

I have now also tried to use a WPF application instead of the Console application to test whether a lack of UI thread might be the problem. However, I have the same problem in the WPF application (of course then also packaged as .msix) as I do in my console application.

c#
uwp
location
geofence
asked on Stack Overflow Feb 10, 2021 by irispoe • edited Feb 11, 2021 by irispoe

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0