【问题标题】:FileNotFoundException when running a PeriodicTask运行 PeriodicTask 时出现 FileNotFoundException
【发布时间】:2013-12-23 17:07:51
【问题描述】:

我正在尝试在我的应用程序中使用 PeriodicTask,但在调用 OnInvoke() 方法之前它失败了,并出现此异常

System.IO.FileNotFoundException

无法加载文件或程序集“LockscreenAgent,Culture=neutral,PublicKeyToken=null”或其依赖项之一。系统找不到指定的文件。

这是我的代码(我省略了ScheduledAgent 的代码,因为它甚至在创建之前就失败了!):

(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>

你猜到了吗?

【问题讨论】:

    标签: c# xaml windows-phone-8 background-agents periodic-task


    【解决方案1】:

    我认为 LockscreenAgent.dll 在部署到手机的应用目录中不存在。在您的解决方案中,包含前台的项目是否有对包含后台代理的项目的引用?如果是这样,请检查该引用的属性,将 Copy Local 设置为 true。还要检查 DLL 的路径是否正确。

    【讨论】:

      【解决方案2】:

      就我而言,我更改了周期性任务项目的名称。结果在 Bin/Debug 文件夹中,程序集具有旧名称。原因是我忘记在周期性任务项目属性中更改程序集名称和默认命名空间。当我这样做时,程序集的名称是正确的,并且 System.IO.FileNotFoundException 已经消失。

      【讨论】:

        猜你喜欢
        • 2019-10-19
        • 2017-06-05
        • 2017-10-10
        • 2017-03-26
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多