FileNotFoundException when running a PeriodicTask

600 views Asked by At

I'm trying to use a PeriodicTask in my app, but it fails before calling the OnInvoke() method with this exception

System.IO.FileNotFoundException

Could not load file or assembly 'LockscreenAgent, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

Here's my code (I'm omitting the ScheduledAgent's code because it fails even before creating it!):

(App.xaml.cs)

    pblic App(){
       (... default code ...)
       InitializeAgent();
    }

    private const string PeriodicTaskName = "LockscreenAgent";
    private PeriodicTask _periodicTask;

    private async void InitializeAgent()
    {
        //Checks if we need to ask user's permission to set the lockscreen
        if (!LockScreenManager.IsProvidedByCurrentApplication)
        {
            // If you're not the provider, this call will prompt the user for permission.
            // Calling RequestAccessAsync from a background agent is not allowed.
            await LockScreenManager.RequestAccessAsync();
        }
        // User gave us permission, let's start the background agent!
        if (!LockScreenManager.IsProvidedByCurrentApplication) return;            
        // Start the agent
        StartPeriodicAgent();
    }

    private void StartPeriodicAgent()
    {
        // is old task running, remove it
        _periodicTask = ScheduledActionService.Find(PeriodicTaskName) as PeriodicTask;
        if (_periodicTask != null)
        {
            try
            {
                ScheduledActionService.Remove(PeriodicTaskName);
            }
            catch (Exception)
            {
            }
        }
        // create a new task
        _periodicTask = new PeriodicTask(PeriodicTaskName)
        {
            Description = "This is LockscreenPreview image provider app.",
            ExpirationTime = DateTime.Now.AddDays(14)
        };
        try
        {
            // add this to scheduled action service
            ScheduledActionService.Add(_periodicTask);
            // debug, so run in every 30 secs
        #if DEBUG
            ScheduledActionService.LaunchForTest(PeriodicTaskName, TimeSpan.FromSeconds(30));
            Debug.WriteLine("Periodic task is started: " + PeriodicTaskName);
        #endif
        }
        catch (InvalidOperationException exception)
        {
            if (exception.Message.Contains("BNS Error: The action is disabled"))
            {
                // load error text from localized strings
                MessageBox.Show("Background agents for this application have been disabled by the user.");
            }
            if (
                exception.Message.Contains(
                    "BNS Error: The maximum number of ScheduledActions of this type have already been added."))
            {
                // No user action required. The system prompts the user when the hard limit of periodic tasks has been reached.
            }
        }
        catch (SchedulerServiceException)
        {
            // No user action required.
        }
    }

(WMAppManifest.xaml)

<Tasks>
  <DefaultTask Name="_default" NavigationPage="MainPage.xaml" />
  <ExtendedTask Name="BackgroundTask">
    <BackgroundServiceAgent Specifier="ScheduledTaskAgent" Name="LSAgent" Source="LockscreenAgent" Type="LockscreenAgent.ScheduledAgent" />
  </ExtendedTask>
</Tasks>
<Tokens>
  ...
</Tokens>
<Extensions>
  <Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" />
</Extensions>

Any guess?

2

There are 2 answers

0
BobHy On BEST ANSWER

I think the LockscreenAgent.dll is not present in the app directory as deployed to the phone. In your solution, does the project containing the foreground have a Reference to the project containing the background agent? If so, check in the properties of that reference that Copy Local is set true. Also check there that the Path for the DLL is correct.

0
Denis On

In my case, I changed the name of periodic task project. As a result in Bin/Debug folder, assembly has old name. The reason is that I forgot change assembly name and default namespace in periodic task project properties. When I did this the name of assembly was correct and System.IO.FileNotFoundException has gone.

See image