0

I'm building simple win phone 8.1 app which using geofence api and background tasks for controlling, when user enter/leaves some area.
To register background task i implement RegisterBackgroundTask method in App class

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.Suspending += this.OnSuspending;
        this.RegisterBackgroundTask();
    }

    private async void RegisterBackgroundTask()
    {
        const string name = "GeofenceBackgroundTask";

        if (BackgroundTaskRegistration.AllTasks.Any(task => task.Value.Name == name))
        {
            return;
        }

        var loc = await new Geolocator().GetGeopositionAsync(
            TimeSpan.FromMinutes(2),
            TimeSpan.FromSeconds(5));   //needed to trig user acceptance

        var backgroundAccessStatus =
            await BackgroundExecutionManager.RequestAccessAsync();

        if (backgroundAccessStatus != BackgroundAccessStatus.Denied)
        {
            var geofenceTaskBuilder = new BackgroundTaskBuilder()
            {
                Name = name,
                TaskEntryPoint = "RingtoneManager.Background.GeofenceBackgroundTask"
            };

            geofenceTaskBuilder.SetTrigger(new LocationTrigger(LocationTriggerType.Geofence));
            geofenceTaskBuilder.Register();
        }
    }

And this is the part, which raise the exception new LocationTrigger(LocationTriggerType.Geofence)

Exception details:

System.InvalidCastException was unhandled by user code
HResult=-2147467262
Message=Unable to cast object of type 'System.__ComObject' to type   'Windows.ApplicationModel.Background.ILocationTriggerFactory'.
Source=mscorlib
StackTrace:
   at System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget)
   at Windows.ApplicationModel.Background.LocationTrigger..ctor(LocationTriggerType triggerType)
   at RingtoneManager3.App.<RegisterBackgroundTask>d__2.MoveNext()

What i have figure out so far:

  1. Exception code is 80004002 (E_NOINTERFACE)
  2. I've investigate what is this interface and found, that it declared in C:\Program Files (x86)\Windows Phone Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd and its actually there ildasm window

    And its referenced from visual studio project solution references reference properties

  3. Every other triggers (SystemTrigger, MaintenanceTrigger e.t.c) are instantiating fine

What i have already tried:

  1. Reinstall VS
  2. Clean/Rebuild solution
  3. Annotating method RegisterBackgroundTask with [STAThread]

This is my first app on windows phone and on c#, so i may do some stupid mistakes in common place. I'm also dont understand how visual studio process those references from solution and how interfaces, that coded in referenced .winmd files, became available to code in project. Maybe there is where something goes wrong. So i need help in searching the root of problem and finding the solution.

Thank you in advance

mshipov
  • 196
  • 1
  • 7
  • You [already asked](http://stackoverflow.com/questions/37043612/new-locationtriggerlocationtriggertype-geofence-fails-with-invalidcastexceptio) this question. Time to call Microsoft Support next. – Hans Passant May 14 '16 at 09:17
  • Now i provide different info at different angle.. Maybe know it would seems more common and understandable for somebody expirienced – mshipov May 14 '16 at 09:33

1 Answers1

0

It could be that "LocationTrigger" is a singleton? (sorry, don't know phone) and has (already) been activated elsewhere with a generic System.__ComObject RCW. If this is the case it cannot be cast. Try using Activator.CreateInstance instead.

Type t = Type.GetTypeFromProgID("WhateverTheProgIdIsHere");
System.Object obj = Activator.CreateInstance(t);
ILocationTriggerFactory tf = obj as ILocationTriggerFactory;