由于UnauthorizedAccessException,.NET ClickOnce应用程序终止

时间:2013-10-08 19:54:27

标签: .net clickonce unauthorizedaccessexcepti

我们写了一个ClickOnce应用程序,可以在办公室的所有PC上运行。今天,.NET运行时崩溃并在事件日志中记录了以下事件:

Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.UnauthorizedAccessException
Stack:
    at System.IO.Directory.DeleteHelper(System.String, System.String, Boolean)
    at System.IO.Directory.Delete(System.String, System.String, Boolean)
    at System.IO.Directory.Delete(System.String, Boolean)
    at System.Deployment.Application.TempFile.DisposeUnmanagedResources()
    at System.Deployment.Application.DisposableBase.Finalize()

应用程序确实使用System.Deployment定期检查新版本并在后台下载。这样,新版本可以在下次启动应用程序时使用。

我的BackgroundUpdater类看起来像这样(如果它是相关的):

class BackgroundUpdaterService : IBackgroundUpdaterService
{
    private readonly BackgroundWorker backgroundWorker = new BackgroundWorker();
    private readonly DispatcherTimer checkForUpdatesTimer = new DispatcherTimer();

    public BackgroundUpdaterService()
    {
        this.backgroundWorker.WorkerSupportsCancellation = true;
        this.backgroundWorker.DoWork += (s, e) =>
            {
                // Check if the application was deployed via ClickOnce.
                if (!ApplicationDeployment.IsNetworkDeployed)
                {
                    e.Result = UpdateStatus.NotDeployedViaClickOnce;
                    return;
                }

                if (this.backgroundWorker.CancellationPending) return;

                var updateCheck = ApplicationDeployment.CurrentDeployment;

                UpdateCheckInfo info;
                try
                {
                    info = updateCheck.CheckForDetailedUpdate();
                }
                catch (DeploymentDownloadException)
                {
                    e.Result = UpdateStatus.DeploymentDownloadException;
                    return;
                }
                catch (InvalidDeploymentException)
                {
                    e.Result = UpdateStatus.InvalidDeploymentException;
                    return;
                }
                catch (InvalidOperationException)
                {
                    e.Result = UpdateStatus.InvalidOperationException;
                    return;
                }

                if (this.backgroundWorker.CancellationPending) return;

                if (info.UpdateAvailable)
                {
                    e.Result = info.IsUpdateRequired 
                        ? UpdateStatus.UpdateRequired 
                        : UpdateStatus.UpdateAvailable;
                }
                else
                {
                    e.Result = UpdateStatus.NoUpdateAvailable;
                }
            };
        this.backgroundWorker.RunWorkerCompleted += (s, e) =>
            {
                var result = (UpdateStatus) (e.Result);
                this.UpdateRequired =
                    result == UpdateStatus.UpdateAvailable
                    || result == UpdateStatus.UpdateRequired;
                if(!this.UpdateRequired) // stop looking if there is one
                {
                    this.checkForUpdatesTimer.Start();
                }
            };

        this.checkForUpdatesTimer.Interval = new TimeSpan(hours: 0, minutes: 15, seconds: 0);
        this.checkForUpdatesTimer.Tick += (s, e) =>
            {
                this.checkForUpdatesTimer.Stop();
                this.backgroundWorker.RunWorkerAsync();
            };
        this.checkForUpdatesTimer.Start();
    }

    private readonly object updateRequiredLock = new object();
    private bool updateRequired;
    public bool UpdateRequired
    {
        get
        {
            lock(this.updateRequiredLock)
            {
                return this.updateRequired;
            }
        }
        private set
        {
            lock(this.updateRequiredLock)
            {
                this.updateRequired = value;
            }
        }
    }

    public bool Update()
    {
        if(!this.UpdateRequired)
        {
            return false;
        }
        bool result;
        try
        {
            var updateCheck = ApplicationDeployment.CurrentDeployment;
            updateCheck.Update();
            result = true;
        }
        catch (DeploymentDownloadException)
        {
            result = false;
        }
        return result;
    }

    public void Dispose()
    {
        this.checkForUpdatesTimer.Stop();
        this.backgroundWorker.CancelAsync();
        Thread.Sleep(100);
        this.backgroundWorker.Dispose();
    }
}

其他人从System.Deployment命名空间遇到此异常?这可能是什么原因?

1 个答案:

答案 0 :(得分:1)

是的,我们遇到了这个问题:)请在此处查看我的答案:https://stackoverflow.com/a/13711535/1246870 - 在2个字中,它是ClickOnce中与频繁检查更新相关的错误。

我能想出的唯一解决方法是将计时器间隔增加到更大的值。如果你有任何更好的想法,我很乐意听到它们。