1

I was developing application with Geofencing, when stuck at issue:

ArgumentException (Value does not fall within the expected range) when I am trying to register background task for geofencing.

Sample from MSDN has the same problem. I will show code from sample for clarity (link to sample: http://code.msdn.microsoft.com/windowsapps/Geolocation-2483de66#content use scenario 5, http://msdn.microsoft.com/en-us/library/windows/apps/dn440583.aspx - instruction how to test).

All what I have done:

  1. Build my app in Visual Studio.
  2. Deploy the app locally first and add the app to the lock screen in Settings.
  3. Close your app that is running locally.
  4. Launch your app in the Visual Studio simulator.
  5. Call RegisterBackgroundTask(..) (simply pressed button register in scenario 5)

There is code from MSDN sample that I have commented for successful app deploying in Simulator - tagged with ////// [my changes] ////////

    async private void RegisterBackgroundTask(object sender, RoutedEventArgs e)
    {
        try
        {

            // Get permission for a background task from the user. If the user has already answered once,
            // this does nothing and the user must manually update their preference via PC Settings.
            //BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();  ////// [my changes] ////////

            // Regardless of the answer, register the background task. If the user later adds this application
            // to the lock screen, the background task will be ready to run.
            // Create a new background task builder
            BackgroundTaskBuilder geofenceTaskBuilder = new BackgroundTaskBuilder();

            geofenceTaskBuilder.Name = SampleBackgroundTaskName;
            geofenceTaskBuilder.TaskEntryPoint = SampleBackgroundTaskEntryPoint;

            // Create a new location trigger
            var trigger = new LocationTrigger(LocationTriggerType.Geofence);

            // Associate the locationi trigger with the background task builder
            geofenceTaskBuilder.SetTrigger(trigger);

            // If it is important that there is user presence and/or
            // internet connection when OnCompleted is called
            // the following could be called before calling Register()
            // SystemCondition condition = new SystemCondition(SystemConditionType.UserPresent | SystemConditionType.InternetAvailable);
            // geofenceTaskBuilder.AddCondition(condition);

            // Register the background task
            geofenceTask = geofenceTaskBuilder.Register();

            // Associate an event handler with the new background task
            geofenceTask.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);

            UpdateButtonStates(/*registered:*/ true);

            ////// [my changes] ////////
            //switch (backgroundAccessStatus)
            //{
            //    case BackgroundAccessStatus.Unspecified:
            //    case BackgroundAccessStatus.Denied:
            //        rootPage.NotifyUser("This application must be added to the lock screen before the background task will run.", NotifyType.ErrorMessage);
            //        break;

            //    default:
            //        // Ensure we have presented the location consent prompt (by asynchronously getting the current
            //        // position). This must be done here because the background task cannot display UI.
            //        GetGeopositionAsync();
            //        break;
            //}
            ////// [my changes] ////////
        }
        catch (Exception ex)
        {
            // HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) == 0x80070032
            const int RequestNotSupportedHResult = unchecked((int)0x80070032);

            if (ex.HResult == RequestNotSupportedHResult)
            {
                rootPage.NotifyUser("Location Simulator not supported.  Could not get permission to add application to the lock screen, this application must be added to the lock screen before the background task will run.", NotifyType.StatusMessage);
            }
            else
            {
                rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage);
            }

            UpdateButtonStates(/*registered:*/ false);
        }
    }

Exception falls on string: geofenceTask = geofenceTaskBuilder.Register();

Can anybody help me?

P.S. Same question thread on msdn - http://social.msdn.microsoft.com/Forums/en-US/3d69f2f9-93e0-401b-8a13-598dc671fa4f/backgroundtask-register?forum=winappswithcsharp

1 Answer 1

2

This is a known "bug" see Windows Store 8.1 Location background tasks do not work in simulator. I have yet to get an answer other than

Your issue has been routed to the appropriate VS development team for investigation

Please upvote it!

3
  • You have another problem, and, I think you simply need to comment few strings (////// [my changes] //////// in my code sample) BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); - it will be needed on thre real device, not simulator.
    – CatCap
    Nov 19, 2013 at 8:22
  • This repro steps? "Download the Geolocation sample found on msdn (code.msdn.microsoft.com/windowsapps/Geolocation-2483de66). Open Visual Studio 2013 RC and debug the application on the location machine to enable the Geofence Background Task (scenario 5). Stop debugging. Start debugging on the simulator. Change the dropdown to scenario 5 (background geofencing) and get error in the OnNavigatedTo method." I read, you didn't say anything about BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); That's why, I think you did not remove it
    – CatCap
    Nov 19, 2013 at 9:52
  • The problem is the same. location background agents fail in the simulator Nov 19, 2013 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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